Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a variable from another method

Tags:

methods

c#

call

I'm new to C# and I really need to know how to call/use a string from another method.

For example:

public void button1_Click(object sender, EventArgs e)
{ 
    string a = "help";
}

public void button2_Click(object sender, EventArgs e)
{
    //this is where I need to call the string "a" value from button1_click 
    string b = "I need ";
    string c = b + a;          
}

In this example I need to call string "a" defined in function button1_Click() from function button2_Click()

like image 236
Zbone Avatar asked Feb 15 '12 20:02

Zbone


People also ask

How do you call a variable from one method to another in Java?

We can't directly pass the whole method as an argument to another method. Instead, we can call the method from the argument of another method. // pass method2 as argument to method1 public void method1(method2()); Here, the returned value from method2() is assigned as an argument to method1() .

How do you reference another method in Java?

We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.

Can we use local variable in another method?

Variables declared inside a method are called local variables. Local variables can only be used within the method body. In this example, I've written two methods. In addition to main(), I've also written foo().


1 Answers

Usually you'd pass it as an argument, like so:

void Method1()
{
    var myString = "help";
    Method2(myString);
}

void Method2(string aString)
{
    var myString = "I need ";
    var anotherString = myString + aString;
}

However, the methods in your example are event listeners. You generally don't call them directly. (I suppose you can, but I've never found an instance where one should.) So in this particular case it would be more prudent to store the value in a common location within the class for the two methods to use. Something like this:

string StringA { get; set; }

public void button1_Click(object sender, EventArgs e)
{ 
   StringA = "help";
}

public void button2_Click(object sender, EventArgs e)
{
    string b = "I need ";
    string c = b + StringA;
}

Note, however, that this will behave very differently in ASP.NET. So if that's what you're using then you'll probably want to take it a step further. The reason it behaves differently is because the server-side is "stateless." So each button click coming from the client is going to result in an entirely new instance of the class. So having set that class-level member in the first button click event handler won't be reflected when using it in the second button click event handler.

In that case, you'll want to look into persisting state within a web application. Options include:

  1. Page Values (hidden fields, for example)
  2. Cookies
  3. Session Variables
  4. Application Variables
  5. A Database
  6. A Server-Side File
  7. Some other means of persisting data on the server side, etc.
like image 137
David Avatar answered Oct 15 '22 09:10

David