Results 1 to 5 of 5

Thread: public variables calling in different forms C#

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    fgh
    Posts
    332

    public variables calling in different forms C#

    I have two forms

    form1 and form2

    form1 is the main form.

    i have created variable in form1 named "getName" and set the value with "Computers"

    now on loading form2 on click of button in the form1, on the load event i want show the value of string : getName in the form1 ....
    gh

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: public variables calling in different forms C#

    Take a look at these threads. The will give you the answers that you need. Good luck.
    http://vbforums.com/showthread.php?t=481564
    http://vbforums.com/showthread.php?t...nitive+passing

  3. #3
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: public variables calling in different forms C#

    If you simply want to display a string from one form onto another. i would pass it through on the constructor. Probably not the best way but it will get the job done.

    Form 1 Code
    Code:
    private void form1Button_Click(object sender, EventArgs e)
            {
                string passString = "this is our string";
                Form2 myForm = new Form2(passString);
                myForm.ShowDialog(this);
            }
    Form 2 Code using the constructor.

    Code:
    public Form(string stringFromForm1)
    {
          _form1String = stringFromForm1;
          InitializeComponent();
    }
    
    private string _form1String;
    public string form1String
    {
         get { return _form1String; }
         set { _form1String = value; }
    }
    I used a property on form 2 even though you wouldn't have to.

    hope this helps.

    -zd

  4. #4
    Junior Member
    Join Date
    Feb 2006
    Posts
    24

    Re: public variables calling in different forms C#

    Hey,

    How would you do this if you are trying to pass an array to another form??

    Thanks,

    Dylan

  5. #5
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: public variables calling in different forms C#

    get() and set() methods are the best ways to access fields/attributes of other classes/forms. You should never make fields public, since it can/will make your classes depend on how this class is implemented, also called high coupling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width