flask
flask

Reputation: 137

How to reference a variable in method parameter?

Sorry for the title if it is inaccurate, I don't really know the name of what I'm looking for

Suppose I have this class:

public class Potion(){

public int WaterAmount;
public int ReagentAmount;

}

Now if I wanted a method to check the amount of water or the amount of reagent in the potion I would have:


    public int GetWaterAmount ( Potion pot ){

    return pot.WaterAmount;

    }
    public int GetReagentAmount ( Potion pot ){

    return pot.ReagentAmount;

    }


Now my question is how can I combine these 2 methods into one so that I can just enter the parameter of the liquid I want to check for? Here's some invalid syntax of what I was looking for:

    public int GetAmount ( Potion pot, int SelectedLiquid){

    return pot.SelectedLiquid;

    }

    void main(){

    GetAmount(pot, WaterAmount);
    GetAmount(pot, ReagentAmount);
    }

In essence how can I make a parameter (selectedliquid) refer to different variables in a class (wateramount or reagentamount) ?

Or is this not possible and I do need to have 1 method for each variable I want to check for?

Upvotes: 0

Views: 70

Answers (3)

Pad
Pad

Reputation: 47

The question is why do you need something like that? Why don’t you just access the property of the object? One Solution can be the above one from thomai. Another option is to give the method a property selector as parameter.

Func<Potion, int> valueSelector

Upvotes: 0

Komediruzecki
Komediruzecki

Reputation: 21

With little bit of logic you could do it. The parameter 'SelectedLiquid' could be boolean in this case or int, and you could check if this value has some appropriate value which refers to the needed class variable. In general this is not a good idea, you don't want to have multiple if-else questionaires or similar switch statements. Better would be to define an enum which will then refer to particular class variable, but even this will have to have some logic based on switch or if-else to retrieve the appropriate value in GetAmount method.

Even better would be to really have two methods which would get the variables you desire. But you need to rethink if you need those getters or you want the class to do something else for you. See if this is an option: What is the { get; set; } syntax in C#?

To repeat:

  1. You can do this based on additional info, use enumeration and if-else/switch to retrieve appropriate property

  2. Better would be to have those particular methods but maybe with public accessors, see the provided link: What is the { get; set; } syntax in C#?

  3. Try to structure your solution based on this article: https://www.javaworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html

Upvotes: 0

Tho Mai
Tho Mai

Reputation: 886

You're looking for an enumeration.

enum LiquidType
{
    Water,
    Reagent
}

public int GetAmount ( Potion pot, LiquidType type)
{
    switch (type)
    {
       case LiquidType.Water:
           return pot.WaterAmount;
       case LiquidType.Reagent:
           return pot.ReagentAmount;
       default:
           return 0;
    }

void main(){

GetAmount(pot, LiquidType.Water);
GetAmount(pot, LiquidType.Reagent);
}

Upvotes: 1

Related Questions