简体   繁体   中英

C# function by passing parameters

Calculate ac# function by passing parameters

My function is something like this:

Result<double> Evaluate(string var, Result<double> height, Result<double> perm)

Var can be anything user wants to enter. For example, if the user enters Depth, then the calculation would be like height * perm and return the var. If user enters SC the calculation is height/ perm and result is stored in var and returns the result

I tried something like this:

List<double> calvar = new List<double>
Switch(var){
    Case “depth”:
        calvar = height * perm
        return calvar
}

I got an error saying * cannot be used for operators of type

You got this error, because you're trying to multiply to objects of the type Result and not the double values itself. If you want to use the * operator for objects of the type Result you have to overload the * operator in your class.

public class Result<T>
{
  public T Value { get; set; }

  public static Result<T> operator * (Result<T> a, Result<T> b)
  {
    // Multiply Logic
  }
}

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