Click to See Complete Forum and Search --> : passing parameters using REF


simon29
January 7th, 2003, 07:31 AM
Hi!

I need to pass some parameters from one window form to another. These parameters need to be changed in the second window, and the changes need to be reflected in the first window. Therefore I use the REF syntax. Everythings seems to run, but when I close the second window, any change has been made to the parameters in the first one. Anyone can have a look to the code.

Many thanks in advance

simon

CODE----------------------------------------------------------------------

/*this part of code is what I do in the first window to create an instance of the second window:*/

ConnectionForm connWindow = new ConnectionForm(ref __sqlServer, ref __sqlServerUser, ref __sqlServerPassword, ref __sqlServerDatabase);


connWindow.ShowDialog();
connWindow.Dispose();

/*This part is in the second window, where I store the parameters in my own variables (not initialized) to be able to change their values later*/

private string m_server, m_user, m_password, m_database;

public ConnectionForm(ref string server, ref string user, ref string password, ref string database)
{
InitializeComponent();

m_server= server;
m_user= user;
m_password= password;
m_database= database;
}

-----------------------------------------------------------------------------------

The problem appears when I close this form, the parameters of the window which calls the second window donīt change.

WillemM
January 7th, 2003, 09:50 AM
I use this kind of functionality too, but this way:

class Form2: System.Windows.Form
{
...
...

private string m_username;
private string m_password;
private string m_db;
private string m_server;

public string username
{
get
{
return m_username;
}
set
{
m_username = value;
}
}

public string password
{
get
{
return m_password;
}
set
{
m_password = value;
}
}

public string database
{
get
{
return m_db;
}
set
{
m_db = value;
}
}

public string server
{
get
{
return m_server;
}
set
{
m_server = value;
}
}

...
...
}

Then I call this function:

public void Test()
{
string a;
string b;
string c;
Form2 frm = new Form2();

frm.username = "bleh1";
frm.password = "bleh2";
frm.server = "bleh3";
frm.database = "bleh4";


if(frm.ShowDialog == DialogResult.OK)
{
a = frm.username;
b = frm.password;
c = frm.database;
}

If you use properties in your connected form you will be able to change all the parameters from the form. Also with a function like yours:

public Form2 (string usr, string pwd, string db, string srvr)
{
m_username = usr;
.....
}

but one BIG warning: DON'T DISPOSE THE CONNECTED FORM BEFORE YOU READ ALL THE PROPERTIES !!!

I hope this will help

}

pareshgh
January 7th, 2003, 10:53 AM
WillemM 's code is very nice approach. since only public stuffs are accessed and that's the good way of doing it.

Paresh