Zbone
Zbone

Reputation: 507

Referencing a variable from another method

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

Upvotes: 16

Views: 166938

Answers (10)

sivabalan
sivabalan

Reputation: 19

you can use session here

public void button1_Click(object sender, EventArgs e)
{ 
string a = "help";
Session["a"]=a;
}
public void button2_Click(object sender, EventArgs e)
{ 
string d=Session["a"].ToString();        
string b = "I need ";
string c = b + d;          
}

Upvotes: 1

northpole
northpole

Reputation: 10346

Make it a class-level variable (global variable), or create a getter and setter for String a, to name a couple options.

Upvotes: 1

Shubham Khare
Shubham Khare

Reputation: 113

I prefer to create a class of required entities and then use them in entire solution without passing variable as argument.

Classname.variableName;

for example:

Class argumentData {
    public static string firstArg= string.Empty;
    public static string secArg= string.Empty;
}

Say I am assigning data in function

void assignData() {
    argumentData.firstArg="hey";
    argumentData.secArg="hello";
}

if I want to use it in another method then

void showData() {
    Console.WriteLine("first argument"+argumentData.firstArg);
    Console.WriteLine("sec argument"+ argumentData.secArg);
}

Upvotes: 7

AwesomeHemu
AwesomeHemu

Reputation: 11

You could save the variable into a file, then access the file later, like this:

public void button1_Click(object sender, EventArgs e)
{ 
    string a = "help";
    File.WriteAllText(@"C:\myfolder\myfile.txt", a); //Change this to your real file location
}

public void button2_Click(object sender, EventArgs e)
{
    string d = File.ReadAllText(@"C:\myfolder\myfile.txt");

    //this is where I need to call the string "a" value from button1_click 
    string b = "I need";
    string c = b + d; //Instead of a, put the variable name (d in this case)          
}

If you do that, just make sure to put this in your code: using System.IO;

Upvotes: -2

David
David

Reputation: 219037

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.

Upvotes: 44

myermian
myermian

Reputation: 32515

class SomeClass
{
    //Fields (Or Properties)
    string a;

    public void button1_Click(object sender, EventArgs e)
    { 
        a = "help"; //Or however you assign it
    }

    public void button2_Click(object sender, EventArgs e)
    {
        string b = "I need";
        string c = b + (a ?? String.Empty); //'a' should be null checked somehow.
    }
}

Upvotes: 2

BlackBear
BlackBear

Reputation: 22989

You can't do this because those variables are in different scopes (think it as being hidden). The only way to achieve this is to move a in the main form class:

public partial class Form1 : Form
{
    string a;

    // etc ...
}

Upvotes: 1

Yuck
Yuck

Reputation: 50855

Refactor that into a method call (or property) so you can access the value of a elsewhere in your application:

public String GetStringAValue() {
    return "help";
}

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

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

Also note that you could be using implicit type declarations. In effect, these are equivalent declarations:

string a = GetStringAValue();
var a = GetStringAValue();

Upvotes: 4

DaveShaw
DaveShaw

Reputation: 52808

You need to declare string a in the scope of the class, not the method, at the moment it is a "local variable".

Example:

private string a = string.Empty;

public void button1_Click(object sender, EventArgs e) 
{  
    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;           
} 

You can now access the value of your "private field" a from anywhere inside your class which in your example will be a Form.

Upvotes: 15

phoog
phoog

Reputation: 43056

You can't do that. string a is a local variable declaration. It's called "local" because it is only accessible "locally" to the block in which it occurs.

To make the variable visible to both methods, you can create a field in the class containing the methods. If the methods are in different classes, though, the solution gets more complicated.

Upvotes: 1

Related Questions