?
Solved

changing value of a second form

Posted on 2006-05-16
12
Medium Priority
?
215 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:yanci1179
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16690365
It would be better (from an OO perspective) to expose the field via a property rather than simply making the field Public.
0
 
LVL 28

Expert Comment

by:2266180
ID: 16690372
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
 

Author Comment

by:yanci1179
ID: 16690529
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1200 total points
ID: 16690557
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
 
LVL 6

Assisted Solution

by:PoeticAudio
PoeticAudio earned 800 total points
ID: 16690671
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
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16690678
Carl beat me to the punch =)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16690701
Interesting that we both use the same form for member variable names tho ;o)
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16690767
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
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16691307
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
 
LVL 28

Expert Comment

by:2266180
ID: 16691407
you 2 are a bit off-topic :) maybe you should move it to the lounge ;)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16691412
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
 

Author Comment

by:yanci1179
ID: 16691527
thanks guys for all your help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

839 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