I have two seperate classes in a project with no inheritance. Is there any way to access a string which is declared in class 2.
For example.
In class 1 I have a string called HiddenWord.
In class 2 I have an int called AllowableMisses
I want to be able to declare an if statement in class 2 saying:
if (HiddenWord.Length > 5)
{
Allowable misses = 5
}
Is this possible?
Loading
AlanPosted Sep 24, 2008, 4:48 PM
It's possible if the string is exposed by class 1 as either a public or internal property or field.
If it's a static property/field, you'd access it from another class with syntax such as this:
Class1.HiddenWord
If it's an instance property/field, you'd need to create an instance first:
Class1 c1 = new Class1();
and then use c1.HiddenWord.
It's also possible to access private or protected fields or properties from another class using a technique known as reflection though I won't go into that unless you particularly want me to.