How would it be possible to reference an external string in an external class.
e.g.
Class1.cs:
MessageBox.Show(mystring);
Class2.cs:
public static void myMethod()
{
string mystring = "foobar";
// some logic here
}
If you create a new instance of Class2 you can make MyString public or pull it out into a get method:
//In Class1
Class2 class2 = new Class2();
MessageBox.Show(class2.Mystring());
//In Class2
public string Mystring{ get; set; }
Or you could return the string from the method
public static string myMethod()
{
string myString = "foobar";
//logic goes here
return myString;
}
//In Class1
Class2 class2 = new Class2();
MessageBox.Show(class2.MyMethod());
Based on your clarification to your question:
I am trying to check a boolean value in a method in class2. E.g. if the method run in class2 changes the boolean value in that method, the method in class1 can check this and do some logic
You could do something like this:
class Class1 {
Class2 myClass = new Class2();
public void ActivityMethod() {
myClass.MethodThatMayChangeBoolean();
if(myClass.myBoolean) {
// Response to a truth change.
} else {
// Respond to a false change.
}
}
}
class Class2 {
public boolean myBoolean { get; }
public void MethodThatMayChangeBoolean() {
// Do stuff in here that may change boolean.
}
}
You will need to use Properties
private static string _mystring = "foobar";
public static string mystring
{
get { return _mystring ; }
set { _mystring = value; }
}
Or use auto properties and initialize their values in the static constructor of the class:
public static string mystring { get; set; }
public static MyStaticClass()
{
mystring = "foobar";
}
public static stringto the class in order to access it from outside the class.