Solved

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

Posted on 2006-11-06
5
192 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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