Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now