C # referencing a variable from another method

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

For instance:

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

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

Thanks!!

+4
source share
8 answers

Normally you would pass it as an argument, for example:

 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. Usually you do not call them directly. (I suppose you can, but I never found an instance where you need to.) Thus, in this particular case, it would be wiser to store the value in a common place in the class for two methods. Something like that:

 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 differently in ASP.NET. Therefore, if you use what you need, you probably want to take it one step further. The reason it behaves differently is because the server side is "stateless." Thus, each button click coming from the client will lead to a completely new instance of the class. Thus, if you set this class element in the first button event handler, it will not be displayed when it is used in the second button event handler.

In this case, you will want to view the state stored in the web application. Options include:

  • Page values โ€‹โ€‹(hidden fields, for example)
  • Cookies
  • Session Variables
  • Application variables
  • Database
  • Server file
  • Some other server-side storage methods, etc.
+24
source

You need to declare string a in the class scope, 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; } 

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

+7
source

Refactoring in a method call (or property) so that 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 can use implicit type declarations. These are essentially equivalent declarations:

 string a = GetStringAValue(); var a = GetStringAValue(); 
+1
source

make is a class level variable (global variable) or create getter and setter for String a to name a couple of parameters.

0
source

You cannot do this. string a is a local variable declaration. It is called "local" because it is only available "locally" for the block in which it occurs.

To make a variable visible to both methods, you can create a field in the class containing the methods. However, if the methods are in different classes, the solution becomes more complex.

0
source

You cannot do this because these variables are in different areas> (consider this hidden). The only way to achieve this is to move a to the main form class:

 public partial class Form1 : Form { string a; // etc ... } 
0
source
 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. } } 
0
source

You can save the variable to a file, then access the file later, for example:

 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 this, just put this in your code: using System.IO;

-2
source

Source: https://habr.com/ru/post/1396722/


All Articles