locked
Using Variable From Another Class RRS feed

  • Question

  • Hi, i have problem with using another class modified variables. For example;

    class classone
    {
      string bla;
      public void functionone()
      {
       bla="hello";
      } 
    }
    
    class classtwo
    {
      classone c1= new classone();
      MessageBox.Show(c1.bla);
    }
    I can't show string bla from second class. If i declare it direclty on class, it works. but if i modified it in a function, from second class i can't get value of string.
    Tuesday, July 6, 2010 9:54 PM

Answers

  • You need to make the property static, so there is only one copy. Also it defaults to internal, make it public.

     

     


    William Wegerson (www.OmegaCoder.Com)
    • Proposed as answer by Thuru VMVP Wednesday, July 7, 2010 9:51 AM
    • Marked as answer by Bin-ze Zhao Tuesday, July 13, 2010 7:04 AM
    Tuesday, July 6, 2010 10:11 PM
  • Since its accessed through an instance of classone, if you make bla static, you get a compiler error. No one asked for there to be only one copy.

    Making it public is enough.

    • Proposed as answer by Thuru VMVP Wednesday, July 7, 2010 9:51 AM
    • Marked as answer by Bin-ze Zhao Tuesday, July 13, 2010 7:04 AM
    Wednesday, July 7, 2010 12:43 AM

All replies

  • You need to make the property static, so there is only one copy. Also it defaults to internal, make it public.

     

     


    William Wegerson (www.OmegaCoder.Com)
    • Proposed as answer by Thuru VMVP Wednesday, July 7, 2010 9:51 AM
    • Marked as answer by Bin-ze Zhao Tuesday, July 13, 2010 7:04 AM
    Tuesday, July 6, 2010 10:11 PM
  • Since its accessed through an instance of classone, if you make bla static, you get a compiler error. No one asked for there to be only one copy.

    Making it public is enough.

    • Proposed as answer by Thuru VMVP Wednesday, July 7, 2010 9:51 AM
    • Marked as answer by Bin-ze Zhao Tuesday, July 13, 2010 7:04 AM
    Wednesday, July 7, 2010 12:43 AM
  • Hi 

    In C# if you do not mention any explicit access modifiers the Framework assigns the private access level. That means those variables can be only accessed on that specific classes

    Have a look on this link about the access modifiers

    http://msdn.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx


    Regards Puboo My blog : http://thurupathan.spaces.live.com
    • Proposed as answer by Thuru VMVP Wednesday, July 7, 2010 9:51 AM
    Wednesday, July 7, 2010 8:22 AM
  • Hi,

    Kindly declare variable bla like below

    public string bla ="";

    its becoz bydefault this variable is private for that classone .whihc cannot be used in classtwo.

     

    Thanxss


    Tushar Capoor
    • Proposed as answer by Tushar Capoor Wednesday, July 7, 2010 8:38 AM
    Wednesday, July 7, 2010 8:38 AM