Take variable value in another method

Asked

Viewed 1,436 times

-2

Inside the same file I have 2 methods:

public IActionResult OnGet(){
    CarregarMenu();

    //Gostaria de pegar aqui o valor da variável lcMenuPrincipal

    Return Page();
}

And the other:

public static void CarregarMenu(){
    string lcMenuPrincipal = "";

    lcMenuPrincipal = "Bom Dia Família";
}

How do I pick up, no OnGet(), the value of the variable in the second method CarregarMenu()?

2 answers

3

You want to make a simple return, but in this case you don’t even need a variable:

public IActionResult OnGet() {
    var variavelAqui = CarregarMenu();
    Return Page();
}

public static void CarregarMenu() => string lcMenuPrincipal = "";

If you really have other things and want to leave the variable, but it doesn’t make sense if you don’t have other things in that method (in the presented form don’t do it this way):

public IActionResult OnGet() {
    var variavelAqui = CarregarMenu();
    Return Page();
}

public static void CarregarMenu() {
    string lcMenuPrincipal = "Bom Dia Família";
    return lcMenuPrincipal;
}

It makes no sense to pass a variable by reference as an argument. If you need to pass a value from one place to another, which the question does not show, then you can do this:

using static System.Console;

public class Program
{
    public static void Main() {}
    public static void OnGet() {
        string lcMenuPrincipal = "";
        lcMenuPrincipal  = CarregarMenu(lcMenuPrincipal);
        return;
    }

    public static string CarregarMenu(string lcMenuPrincipal) {
        WriteLine(lcMenuPrincipal); //estou fazendo alguma coisa com esse valor, caso contrário não sentido receber esse parâmetro
        return "Bom Dia Família";
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • 1

    "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 :)

  • 1

    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.

2

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?

  • Thank you for your reply, could I leave an example in the second way?

  • 1

    yes, I edited the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.