Variable visibility in subclasses...
-
Hi all! I have a class like this:
class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } }
Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. Luca -
Hi all! I have a class like this:
class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } }
Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. LucaYou could... pass a reference to your strA to class B through its constructor. Or, you could probably do something with delegates. I must check up on delegates one day. :rolleyes:
My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -
The Undefeated
-
You could... pass a reference to your strA to class B through its constructor. Or, you could probably do something with delegates. I must check up on delegates one day. :rolleyes:
My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -
The Undefeated
-
Hi all! I have a class like this:
class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } }
Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. Luca -
If it actually was a subclass, you could just make the string protected. As B is not a subclass at all, you can't do that. You have to pass a reference to the instance of the class.
Experience is the sum of all the mistakes you have done.
-
devzav wrote:
And how can I do to make B a "real" subclass?
Inherit from the A class:
class B : A {
...
}Experience is the sum of all the mistakes you have done.