Skip to content

Access public class member from other class

hellopeat

Asked Mar 14, 2005, 4:36 AM EST5 answers
Share
LinkedInX
Suppose, i have 2 classes public class A { public string s1; } public class B { } Is there anyway to use string 's1' from class A in class B ? My conditions are: - " string s1 " is non-static variable. ( just public ) - In class B, i will not use " A a = new A(); a.s1 = "hello"; " to access string s1. (not new it).

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi, I don't think it is possible without the usage of "new" or "static". Why do you want to achieve it without those keywords? Cheers.
    0
  • 0
    i mean get and set the value of variable in class A from class B without "A a = new A();" in class B and the variable in class A is non-static variable, just public. is it possible ? if it is possible, how to do it ? Thanks a lot.
    0
  • 0
    Hi, I don't think you can do that. But if you really find out the technique, please do share it with us! Just remember, whenever you code C#, code with OOP in mind. Cheers.
    0
  • 0
    If you want to do that, use a static member. For example, public static string s1; That way you can call A.s1 However, you cannot modify a member of a class when you don't have an instance to modify in the first place, so the only way to do what you're saying would be to use a static member.
    0
  • 0
    Hi, Based on the author of the thread, he does not want to use either static or the new keyword. Hmm.. i still don't understand why code till so complicated? Cheers.
    0