2

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
}
4
  • What is the problem that you are trying to solve? Usually it can be accomplished without doing something like this Commented Sep 12, 2011 at 20:29
  • 1
    You don't. Variables that are declared in a method have a scope limited to that method, so what you're asking for is a technical impossibility. -- You might want to add a public static string to the class in order to access it from outside the class. Commented Sep 12, 2011 at 20:29
  • @skaz 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 Commented Sep 12, 2011 at 20:31
  • 3
    I'm not sure I understand the downvotes on a question like this. Obviously this can't be accomplished (at least not how the question is posted) due to variable scope. But, instead of downvoting the question (as James may be learning), why not answer the question that way and then offer an alternative suggestion. Commented Sep 12, 2011 at 20:31

5 Answers 5

2

If I right understood your question, you can do something like this:

public class2 
{
    public static string MyString 
    {
       get {return "foobar"; }
    }

}


public class1 
{
    public void DoSomething() 
    {
       MessageBox.Show(class2.MyString );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Something like this?

public static class Foo
{
    public const string FOO_CONST = "value";
}

public class Bar
{
    public void Bar()
    {
        Console.WriteLine(Foo.FOO_CONST);
    }
}

Comments

0

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());

Comments

0

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.
  }
}

Comments

0

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";
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.