I have the following static function in c#
public static string Greet(string name)
{
string greeting = "welcome ";
// is it possible to pass this value to a label outside this static method?
string concat = string.Concat(greeting, name);
//error
Label1.text = concat;
//I want to return only the name
return name;
}
As you can see in the comments, I want to retain only the name as the return value, however I want to be able to take out the value of the concat variable to asign it to a label, but when i try the compiler refuses, can it be done? Is there a work around?
Thank you.
If the method must be static for some reason, the main approach here would be to pass any required state into the method – i.e. add a parameter to the method that is either the label or (better) some typed wrapper with a settable property like
.Greeting:(where
YourTypecould be your control, or could be an interface allowing re-use)What you don’t want to do is use static state or events – very easy to get memory leaks etc that way.
For example: