?
Solved

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

Posted on 2006-11-06
5
Medium Priority
?
198 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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