Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 63
MailAddress in vb 4 29
Where is this file? 3 25
Change to file doesn't show up 16 50
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

791 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