Referencing a Variable from Another Method

Referencing a variable from another method

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.

How to call a variable in another method?

First declare your method to accept a parameter:

public void take(String s){
//
}

Then pass it:

public void example(){
String x = "name";
take(x);
}

Using an instance variable is not a good choice, because it would require calling some code to set up the value before take() is called, and take() have no control over that, which could lead to bugs. Also it wouldn't be threadsafe.

C# referencing a methods variables from another method


If they all are in the same class

You can configure them as class variables:

public class MyClass{

//set this as private/protected/public or nothing and you can also set a default value
int a;

public void A()
{
a = 4;
string b = "Hello World";

B();

C();
}

public void B()
{
a = 3;
...
}

public void C()
{
a = 3;
...
}
}


Elseway

public static class MyClassA{
public static int a = 0;

public static void MethodA(){
this.a = 3;
}
}

now from method B you can access MyClassA

int myExValueA = MyClassA.a;

Elseway you gotta pass them as parameters

hope this helps

How do i call on a local variable from another method?

In Java there are things called "scopes" between the parenthesis. A variable that is created in one scope cannot be accessed by another scope. An example is the variable that you are using here. What you can do is you can either call the method from another method and get a return value for "user", or you can pass the variable in one scope as a parameter for another method. I will demonstrate with ar mini example below:

public static void scopeOne() {
String myName = "name";
}

public static void scopeTwo() {
System.out.println(myName);
}

This will obviously not work because you cannot access myName from scopeOne in scopeTwo this way. You will get a compile-time error. You could solve this in various ways, but here is one example:

public static void scopeOne() {
String myName = "name";
scopeTwo(myName);
}

public static void scopeTwo(String myName) {
System.out.println(myName);
}

You could pass the variable to the method as an argument and make the variable a parameter. That way it can be within the local scope of the method you are attempting to call. Another thing you can do is make the User object variable a class or instance variable and then update it within your methods.



Related Topics



Leave a reply



Submit