Access between classes?
-
I have two classes, ClassA and ClassB:
public class ClassA { //Variable definition private int intProperty; //Property definition public int Property { get { return intProperty; } set { intProperty = value; } } //Constructor public ClassA() { ClassBTest = new ClassB(); } }
My question is, how do I access and modify ClassA.Property from ClassB? For example (understanding that this doesn't actually work):public class ClassB() { private int Function() { int Temp = ClassA.Property; Temp++ ClassA.Property = Temp; } }
There's got to be a fundamental way to do this, I'm just missing that piece of information. Thanks for your help. -
I have two classes, ClassA and ClassB:
public class ClassA { //Variable definition private int intProperty; //Property definition public int Property { get { return intProperty; } set { intProperty = value; } } //Constructor public ClassA() { ClassBTest = new ClassB(); } }
My question is, how do I access and modify ClassA.Property from ClassB? For example (understanding that this doesn't actually work):public class ClassB() { private int Function() { int Temp = ClassA.Property; Temp++ ClassA.Property = Temp; } }
There's got to be a fundamental way to do this, I'm just missing that piece of information. Thanks for your help.what use wrote in your code was the use of a static property. if you use ClassA.Property the property definition must be: public static int Proprty {...} But you can also use is this way you code it. But then you must create an instance of the class to use the property: ClassA c = new ClassA(); int Temp = c.Property;
-
what use wrote in your code was the use of a static property. if you use ClassA.Property the property definition must be: public static int Proprty {...} But you can also use is this way you code it. But then you must create an instance of the class to use the property: ClassA c = new ClassA(); int Temp = c.Property;
That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.
-
That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.
-
then you must use it as static like this: class ClassA { private static int i; public static int Property { get { return i; } set { i = value; } } } then you can use it via int i = ClassA.Property;
-
That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.
Just as an aside - doing what the first reply suggested would not lead to creating a new class. It would lead to creating a new instance of an existing class. Either way (static or instance), you're taking up space on the heap. The difference is that with a static member, all clients in the AppDomain shares a single instance of the static member, and with non-static (instantiation), each call gets a new copy of the object.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’