passing variable from one form to another,in form2,form1 variable are initialized

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parameswar Nayak
    New Member
    • Jul 2010
    • 7

    #1

    passing variable from one form to another,in form2,form1 variable are initialized

    Code:
    Form1--
    public string val="Raj";
    form2 obj=new form2()
    obj.show();
    
    Form2--
    method()
    {
      form1obj=new form1()
      textbox1.text=obj.val;
    }
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    In visual basic they have the ability to create modules where you could store all of your variables sharable between all forms, however in c# we have to be a tiny bit more clever, add a new class to your project and edit it as follows:

    Code:
    public static class MyVariables
    {
      public static string val = "Raj";
    }
    Now to access the variable, all you need to do is this:

    Code:
     //Form2--
    
     void method()
     {
       textbox1.text=MyVariables.val;
     }
    happy days!

    Comment

    • Parameswar Nayak
      New Member
      • Jul 2010
      • 7

      #3
      Originally posted by AmzBee
      In visual basic they have the ability to create modules where you could store all of your variables sharable between all forms, however in c# we have to be a tiny bit more clever, add a new class to your project and edit it as follows:

      Code:
      public static class MyVariables
      {
        public static string val = "Raj";
      }
      Now to access the variable, all you need to do is this:

      Code:
       //Form2--
      
       void method()
       {
         textbox1.text=MyVariables.val;
       }
      happy days!
      Thanks AmzBee

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        If you feel a post has answered your question, please remember to mark it as Best Answer.

        Mary

        Comment

        Working...