changing value of a second form

I am using .net 2.0,

I have to forms and from the first one I need to change the value of one of the variables of the second form.
At first I had the variable on the second form as private.  I would then get and return the value.  From the first form I tried to assign a value to the second form and I got an error saying that the value was read only and it could not be changed.  I then changed the it from private to public and I was able to change the value from the first form fine.  It works, but I was wondering if there is a better way of doing this?  Thanks.
yanci1179Asked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
In C# a property, in its basic form, looks something like:

    class Person
    {
        private string _firstName;

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
    }
0
 
Carl TawnSystems and Integration DeveloperCommented:
It would be better (from an OO perspective) to expose the field via a property rather than simply making the field Public.
0
 
2266180Commented:
well, better would be to using a property :) but then again you will be in the same situation, just using something ... "nicer".

what you should consider are the following:
1) will there be more instances of form2? if so, you need to make sure that the variable in the correct one will be changed AND that the correct instance is assigned to form1's property
2) will there be multiple instances of form1? if so, you have to consider concurrent access to form2
3) will there be multiple instances of both form1 and form2?

in all cases you must analyze the dependencies between the forms and act accordingly.
but in most of the real life situations, there will be only one form1 and one form2, form1 depending on form2 and maybe viceversa. so there is nothing to be worried about. the question is: is this your case? if so, then you don't have a problem to think about :)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
yanci1179Author Commented:
more likely there would be only one form1 and one form2.  However I would like to expose if via a property since I'm trying to get a good grasp of OO.  Any ideas of where I can get an example to start of at?
0
 
PoeticAudioConnect With a Mentor Commented:
here's an example of using properties (just in case you aren't sure)...

I just kept default names, and also this is strictly showing the use of a property. In a lot of cases you would use a public property to access, set a private variable in another class. In this case, the public property in Form2 just accesses the .Text property of a label.

Form2 code (put this where you would put your class-wide variables)
----------------------

//This is just a simple property to access/set the .Text property of label1.
//you can also use a simple variable
public string LabelText
{
      get  {     return label1.Text;           }
      set  {     label1.Text = value;         }
}



Form1 Code
----------------------
private void button1_Click(object sender, System.EventArgs e)
{
    Form2 form2 = new Form2();
    form2.LabelText = "It Worked!";
    form2.Show();
}



Here is an example of using it to get/set a private variable in the Form2 object as well as set
the label text.


private string _labelText;

public string LabelText
{
      get  {    return _labelText;      }      
      set  
      {    
           _labelText = value;
           label1.Text = _labelText;
       }
}
0
 
PoeticAudioCommented:
Carl beat me to the punch =)
0
 
Carl TawnSystems and Integration DeveloperCommented:
Interesting that we both use the same form for member variable names tho ;o)
0
 
PoeticAudioCommented:
yup!

It may not be the best practice, but I only do that with variables that are accessed via properties, other member variables I do in camel-case without the proceeding "_".
0
 
PoeticAudioCommented:
weird, we also do the same format for setting up properties

public whatever Prop
{
     get {  return _prop;      }
     set {  _prop = value;    }
}

most of time I see people set them up like

public whatever Prop
{
    get
    {
         return _prop;
    }
    set
    {
        _prop = value;
    }
}


I like the first method cuz it looks cleaner, plus I can look at more on my screen when i'm scrolling through members.
0
 
2266180Commented:
you 2 are a bit off-topic :) maybe you should move it to the lounge ;)
0
 
Carl TawnSystems and Integration DeveloperCommented:
I tend to always use the single line approach, after all most gets are single line. And then just use the block approach for anything else.
i.e

    public whatever Prop
    {
        get
        {  
             return _prop;
        }
        set
        {
             if (value > 0)
                 _prop = value;    
             else
                 _prop = 0;
        }
    }

I tend to declare all member variables at the bottom of the class as well, although i've only seen a small number of people doing this.

i.e.

    class Person
    {
        public string FirstName
        {
            get { return _firstName; }
            set ( _firstName = value; }
        }
 
        private string _firstName;
    }

One other advantage of prefixing all member variables with _ is that they always appear grouped together at the top of the IntelliSense window.
0
 
yanci1179Author Commented:
thanks guys for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.