Use a Variable from Another Method in C#

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.

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

accessing variables from another method

DaveDev answer is correct move out the variable

public void chkAuto_CheckedChanged(object sender, EventArgs e)
{


if (chkAuto.Checked = true)
{

carPrice = carPrice + 1300;//because is declared inside another method

}

Make carPrice an instance variable

public partial class BMW : Form
{
private CarDatabase database;
private double carPrice;
...

And remove it from the method

private void BMW_Load(object sender, EventArgs e)
{
...
// double carPrice;
...

pass the value of a variable to another method C#

Each HTTP request is treated as a new request, this means that your "code" variable is always cleaned every time it reaches the server, so ASP.NET WebForms offers a way to store temporary values in the session variable.

Your code could see like this:

protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "visitcode")
{
Session["Code"] = e.CommandArgument;
}
}

In a second request, you could retrieve the value as follows:

protected void button1_click(object sender, EventArgs e)
{
...
string visitcode = Session["Code"];
}

As I mentioned, Session variable is temporary so you have to validate if your value is different to NULL, if so this means that the session is over.

I hope it is useful for you

How to use variables from a protected method in another method

Your answer variable is local to the click event handler. It does not exist outside of it. You have to define a field:

int answer;
protected void Button1_Click(object sender, EventArgs e)
{
answer = 5;
}

As a general rule: Everything inside curly braces is visible only within those braces (except when you can define a visibility, like public or internal). This is called scope.

Note that the value is lost once the page has been sent. If you want to preserve the value across requests you have to store the value in a more permanent way, for example in the view state.



Related Topics



Leave a reply



Submit