How to reference a variable in method parameter?

java pass by reference
how to call a method with parameters in java
c# pass by reference
c# pass string by reference
c# reference variable
parameter passing in java with example
java method arguments
c# pass object by reference

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?

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);
}

Pass By Reference, The Twice function takes two integer parameters, and multiplies each by 2. Note that the original variables passed into the function from main() are not affected by � Local Variable – Block Specific Or Method Specific Variables; Static Variable – The class Variables; Instance Variable – The Non Static Variables; Method Parameter; Example of Reference Variable Being Created. In the below example we will take the case of an Employee class which has few primitive variables and we would take the reference

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

Java Method Arguments, Note that the object's instances variables are changed in this case. Why? The reference to id is the argument to the method, so the method cannot be used to� Variables passed as in arguments must be initialized before being passed in a method call. However, the called method may not assign a value or modify the argument. The in parameter modifier is available in C# 7.2 and later.

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

Passing references type variable as method parameter, In Java, primitive types (such as integer) are always handled exclusively by value , and objects (such as your Person) and arrays are always� To make the function work on the actual parameter passed we pass its reference to the function as: void increment(int &input) { // note the & input++; } the change made to input inside the function is actually being made to the actual parameter. This will produce the expected output of 1 2

4. Methods Use Instance Variables: How Objects Behave, Arguments are the things you pass into the methods. An argument (a value like 2, “Foo”, or a reference to a Dog) lands face-down into a wait for it parameter. Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:

Pass-By-Value as a Parameter Passing Mechanism in Java , It means that when a variable is pass-by-reference, the unique identifier of the object is sent to the method. Any changes to the parameter's� In c#, passing a value type parameter to a method by reference means passing a reference of the variable to the method. So the changes made to the parameter inside of the called method will have an effect on the original data stored in the argument variable. By using the ref keyword, we can pass parameters reference-type and it’s mandatory to initialize the variable value before we pass it as an argument to the method in c# programming language.

Function pass by value vs. pass by reference, I will call what you are passing in a to a function the actual parameters, and where this memory holds the formal parameter values and function local variables. A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.