简体   繁体   中英

How can i call upon only the return variable in a method?

I'm new and struggling with object orientated programming. I want to use only the return value in my third method 'tableinfo' however i don't know how to transfer only this value to the other methods, without running the first two methods again. All i want to do is transfer only the value that the user enters over to the third method and not have to put in the values two times each, this is the only way i know to get the value across and i would really appreciate if anyone could help me to just get the return value. This code is a tiny snippet of what i'm trying to do and it's purpose is not important, i just wanted to create an example to try and allow people to understand what i mean. Thank you in advance!

class Program
{
    static void Main(string[] args)
    {
        TableOrder TO = new TableOrder();
        TO.TableNumber();
        TO.NumberOfPartons();
        TO.tableinfo();
    }
}

class TableOrder
{
    int tablenumber;
    string inputtablenumber;
    int numberAtTable;
    string inputNumberAtTable;

    public int TableNumber()
    {
        Console.Write("please enter the table number:");
        inputtablenumber = Console.ReadLine();
        tablenumber = int.Parse(inputtablenumber);
        return tablenumber;            
    }

    public int NumberOfPartons()
    {
        Console.Write("please enter how many people are seated: ");
        inputNumberAtTable = Console.ReadLine();
        numberAtTable = int.Parse(inputNumberAtTable);
        return numberAtTable;
    }               

    public void tableinfo()
    {
        int tablenum = TableNumber();
        Console.Write(tablenumber + 1);

        int patrons = NumberOfPartons();
        Console.WriteLine(numberAtTable + 1);
    }
}

It looks like you might be confused on the difference between methods, properties, and fields. Your function TableNumber() might be more accurately called AskUserForTableNumber() or GetTableNumberFromInput(). Something like that. You are also both setting a member field and returning the value. So there are a bunch of ways you could store and retrieve that value. If the member field tablenumber was marked as public, you could access it. Or in your main function you could do this:

int tablenum=TO.TableNumber();

and then reuse that value.

Another odd thing you are doing is storing the input string as a member field. If you don't need to reference that string again, then there's no reason for that to be a member of the TableOrder object, it could be a local variable to the function that is doing the input.

But it seems like you are trying to use TableOrder.TableNumber like a property. And that very well may be the right thing to do, but not in the way that you are doing it. Here is a (sort of fancy) way of doing something similar, which also uses a concept of lazy-loading...

class TableOrder
{
    private int? _tablenumber;
    public int TableNumber 
    {
        get
        {
            return _tablenumber ?? (_tablenumber=GetTableNumberFromInput());
        }
        set
        {
            _tablenumber = value;
        }
    }

    private static int GetTableNumberFromInput()
    {
        Console.Write("please enter the table number:");
        string inputtablenumber = Console.ReadLine();
        return int.Parse(inputtablenumber);
    }

    //(and so on for other member properties)
}

This way, the first time you try to access table number, it will ask the user for the value. Afterward, you will already have the value, so it will not ask again. Note that this type of approach is not really necessary, and is mainly useful for waiting to load a value until you need to use that value. Instead you could just do something like: TableOrder.TableNumber = GetTableNumberFromInput();

First of all, you can remove the calls in your main since the method tableinfo() will call them:

class Program
{
    static void Main(string[] args)
    {
        TableOrder TO = new TableOrder();
        TO.tableinfo();
    }
}

Second, you want to use the class variables that you already declared, The returned value of the two functions are stored inside those and you can output them with Write .

public void tableinfo()
{
    tablenumber = TableNumber();
    Console.Write(tablenumber + 1);

    numberAtTable = NumberOfPartons();
    Console.WriteLine(numberAtTable + 1);
}

In the scope of this function, the return values ( return numberAtTable and return tablenumber ) don't exist anymore, they are stored in whats left of the called functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM