Solved

How can I change the value of a passed-in parameter's value?

Posted on 2006-11-06
5
190 Views
Last Modified: 2010-04-16
Hi, I have a couple of ref string input variables that I would like to change their value in the Dialog Window but my code below is not able to do that.  I created the         string loginID=null, pswd=null, defUserID;

variables to store the parameters but when I change the value in the  btnOK_Click routine, the calling program doesn't receive the new value.  Thanks.



    public partial class frmUserIDPassword : Form
    {
        string loginID=null, pswd=null, defUserID;

        public frmUserIDPassword(ref string userID, ref string Password, string defUserName)
        {
            InitializeComponent();
            loginID = userID;
            pswd = Password;
            defUserID = defUserName;
        }

-------------------------
        private void btnOK_Click(object sender, EventArgs e)
        {
            loginID = txtUserName.Text;
            pswd = txtPassword.Text;
            this.Close();
        }
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17887401
You try to get them in the form constructor, they are null at this time. You need to write other function for this:

public partial class frmUserIDPassword : Form
    {
        string loginID=null, pswd=null, defUserID;

        public frmUserIDPassword()
        {
            InitializeComponent();
        }

-------------------------
        private void btnOK_Click(object sender, EventArgs e)
        {
            loginID = txtUserName.Text;
            pswd = txtPassword.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public void GetPassword(ref string userID, ref string Password)
        {
            loginID = userID;
            pswd = Password;
       }
}

Client code:

frmUserIDPassword f = new frmUserIDPassword();

if ( f.ShowDialog(this) == DialogResult.OK )
{
    f.GetPassword(ref s1, ref s2);   // get password after form is closed and values are assigned
}
0
 

Author Comment

by:lapucca
ID: 17891260
I want to assing the value gotten from the textboxes to the ref input parameters userID and Password.  I don't see how the public GetPassword can do this by
       public void GetPassword(ref string userID, ref string Password)
        {
            loginID = userID;
            pswd = Password;
       }


Do you mean ?
       public void GetPassword(ref string userID, ref string Password)
        {
         userID   =  loginID ;
        Password    = pswd ;
       }
0
 

Author Comment

by:lapucca
ID: 17891344
Also, The main program pass in the 2 paramenters to the 1st dialog window and then it passes it to the 2nd dialog window to get user's input.  How would I pass it in the 1st dialog window?  Thanks.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17891407
Yes, your correction of GetPassword is what I mean. The point here is that you try to get password from the form constructor, when form is just created. You need to do this after form is closed, this is why I suggest to move these parameters to separate function.
Other way is to do this using properties:

public partial class frmUserIDPassword : Form
{
    ...

    public string LoginID{ get { return loginID; } )
    public string Password{ get { return pswd; } )

    ...
}

Client code:

frmUserIDPassword f = new frmUserIDPassword();

if ( f.ShowDialog(this) == DialogResult.OK )
{
    // use here f.LoginID and f.Password    
}

In such case I prefer properties which are more readable. If you need to pass this to second form, use the same way.
0
 

Author Comment

by:lapucca
ID: 17891647
Thanks Alex.  That worked great!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question