-3

i have this function:

public int func1 (int x, int y)
{
    return Math.Max(x, y);
}

what i want to do is , that function will return me or min or max depend on what i will send as parameters , like:

public static int func1 (int x, int y ,string minMax)
{
    return Math.minMax(x, y);
}

static void Main(string[] args)
{
    string x = "Max";
    Console.WriteLine(func1(1, 2, x));        
}

i want that i can use instend of the properties Max , use on string how can i do this?

thanks!

Christos
  • 53,228
  • 8
  • 76
  • 108
dani1999
  • 89
  • 2
  • 10

5 Answers5

2

This should work :

    public static int func1(int x, int y, string minMax)
    {
        return (int)typeof(Math).GetMethod(minMax, new[] { typeof(int), typeof(int) }).Invoke(null, new object[] { x, y });
    }


    private static void Main(string[] args)
    {
        Console.WriteLine("Max Wanted :" + func1(1, 2, "Max"));
        Console.WriteLine("Min Wanted :" + func1(1, 2, "Min"));
    }
Toto
  • 7,491
  • 18
  • 50
  • 72
  • 1
    based on this Function `GetMethod` I believe that this is the answer that the OP is looking for however.. I doubt the OP can understand this working solution.. good answer btw `+1` – MethodMan Dec 03 '15 at 23:37
0
public static int func1 (int x, int y ,string minMax)
{
    if(minMax == "Min") 
    { 
         return Math.Min(x,y);
    } 
    else if (minMax == "Max") 
    {
        return Math.Max(x,y);
    }
    else 
    {
      // Not sure what you want to do here in your program.
    }
}
wentimo
  • 471
  • 3
  • 12
  • i know if else . but i want what i ask please , use on string as properties – dani1999 Dec 03 '15 at 23:04
  • @David Bachar, could you explain what you mean by using the string as a property more clearly? – wentimo Dec 03 '15 at 23:12
  • @Wentimo this would work if you were to have the proper values returned you should assign a variable outside of the if statement assign it like `var minMaxValue = default(int);` then inside the if statement assign `minMaxValue = Math.Min(x,y) same for Math.Max, then outside return minMaxValue the way your code stands it will generate not `not all paths return` error – MethodMan Dec 03 '15 at 23:31
0

I would suspect

if("Max".Equals(minMax)) {
    // code for max
} else {
    // code for min
}

would work.

I suggest reading up on control structures. It's pretty basic stuff and you really should know how to use them if you don't already.

This should help with if/else structures in C#: https://msdn.microsoft.com/en-us/library/5011f09h.aspx

Also, the reason I put "Max" first is to avoid Nullpointer exceptions.

Koen De Groote
  • 111
  • 1
  • 8
  • i know if else . but i want what i ask please , use on string as properties – dani1999 Dec 03 '15 at 22:59
  • @DavidBachar sounds like you need to show a better representation of what you are looking for .. are you looking to use create a Property..? – MethodMan Dec 03 '15 at 23:33
0

To add to the other answers, personally I would have two separate methods (i.e. just use Math.Max or Math.Min) but if you need one method, I would use a bool instead of a string. One main reason for that is that you avoid case-sensitive issues and since there are only two options, a boolean is suited to that. Alternatively, you could use an Enum.

public static int FindMinOrMax(int x, int y, bool findMax)
{
    return findMax ? Math.Max(x, y) : Math.Min(x, y);
}

EDIT From the other comments, I've understood what you're trying to achieve. Whilst I don't recommend it, you could do this

public static int FindMinMax(int x, int y, string method)
{
    MethodInfo mi = typeof(Math).GetMethod(method, new[] { typeof(int), typeof(int) });
    return (int)mi.Invoke(null, new object[] { x, y} );
}

Method is case-sensitive so Max will work but max won't as the method is called Max. You can't just capitalise the first letter all the time (unless it's just Min/Max) as DivRem has camel-casing.

+1 to Toto as he/she answered first, I just edited and noticed but will keep this post for completion

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • i knew to do that , but i want exactly what i ask – dani1999 Dec 03 '15 at 22:58
  • The other answers have given you what you need but I figured other people may read this question and want an answer that includes something that may be considered better practice. – keyboardP Dec 03 '15 at 23:06
  • no friend .. what i need is use in string as properties – dani1999 Dec 03 '15 at 23:07
  • 1
    @DavidBachar you are making `ZERO SENSE` how can you return `MIN MAX` on a String value of `Max" and if that's the case the create a property called `Max and one called Min` and do your function based on that ... – MethodMan Dec 03 '15 at 23:07
  • @DavidBachar - Sorry, I'm not quite following the question but, from what I understand, the other answers use a string parameter and return what you need. This will require an `if else` statement (or the shorthand I've used which works in the same way as an `if else`). – keyboardP Dec 03 '15 at 23:09
  • @DavidBachar - I've updated my answer – keyboardP Dec 03 '15 at 23:38
0

What you want to do is evoke a function by name using Reflection.

In this case, I would advise using a solution like the one suggested by user keyboardP because it prevents the caller from passing an invalid value.

However, here's exactly what you asked for:

public static int MaxOrMin(int x, int y, string methodName)
{
  Type mathType = typeof(Math);
  MethodInfo method = mathType.GetMethod(methodName);
  if (methodName.ToLower() != "max" || methodName.ToLower() != "min")
    throw new InvalidOperationException("Please specify Max or Min as the method name.");
  object result = method.Invoke(null, new object[] { x, y });
  return (int)result;
}

Reference: This answer

Community
  • 1
  • 1
Tonkleton
  • 547
  • 3
  • 14