You need the variable visibility to be wide for both methods. In this case, you need to declare in the class scope:
class NomeDaSuaClasse
{
// aqui a variável será visível para todos os métodos da classe
string lcMenuPrincipal = "";
public IActionResult OnGet(){
// pode acessar a variável aqui e também no método CarregarMenu
}
}
An alternative would be to declare the variable of the method scope onGet
, and receive by return of the method CarregarMenu
. In that case it could not be void, it would be string.
public IActionResult OnGet(){
string lcMenuPrincipal = CarregarMenu();
}
public static string CarregarMenu(){
return "Bom Dia Família";
}
You can still pass the variable by reference to the method, another solution:
public IActionResult OnGet(){
string lcMenuPrincipal = "";
CarregarMenu(ref lcMenuPrincipal);
}
public static void CarregarMenu(ref string lcMenuPrincipal){
lcMenuPrincipal = "Bom Dia Família";
}
If you want to better understand how a parameter works by reference, see this other question: How to pass string by reference?
"It makes no sense to pass a variable by reference as an argument" It may be Maniero, but as he said he is studying. Net, didactically it would be interesting to know all the options, after all reference is part of the language :)
– Ricardo Pontual
Options yes, but you didn’t give it as an option, then changed to say it’s an option. Actually the question doesn’t make much sense, it’s unclear what he wants, or lacks information of what he really wants or doesn’t need any of this, I only answered because I had an answer talking to complicate something simple, most likely was to close the question by not having a clear real problem.
– Maniero