CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2002
    Posts
    16

    passing parameters using REF

    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.

  2. #2
    Join Date
    Jul 2002
    Location
    .NET 2.0/.NET 3.0/.NET 3.5 VS2005/VS2008
    Posts
    284
    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

    }
    WM.

    What about weapons of mass construction?

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    WillemM 's code is very nice approach. since only public stuffs are accessed and that's the good way of doing it.

    Paresh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured