LAL
LAL

Reputation: 21

How to pass enum as method parameter?

The task is to write a simple method that can sort int array (in ascending or descending order - should be set as enum type parameter of this method). I have written the method itself and enum, but I have no idea how to set enum as method parameter:(

Would be great to get any help from you, guys, cause I am completely new to coding.

class Program
{

    public enum options
    {
        UpSortOption,
        DownSortOption
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[] { 3, 8, 0, 2, 16 };
    }

    static void orderArray(int [] array, options op)
    {
        switch(op)
        {
            case options.UpSortOption:
                Array.Sort(array);
                foreach (int number in array)
                {
                    Console.Write(number + " ");
                }
                break;

            case options.DownSortOption:
                Array.Sort(array);
                Array.Reverse(array);
                foreach (int number in array)
                {
                    Console.Write(number + " ");
                }
                break;

        }
    }
}

Upvotes: 2

Views: 8110

Answers (4)

sujith karivelil
sujith karivelil

Reputation: 29036

The signature of the method looks fine, Now you wanted to call this method by passing the first parameter of type integer array and the second parameter of type options for that you can use the following code:

orderArray(arr,options.UpSortOption);

Or else you can declare a variable of type options and pass that variable, the change you have to make for that case will be:

options optionsVariable = options.DownSortOption;
orderArray(arr,optionsVariable);

Upvotes: 3

Gareth
Gareth

Reputation: 911

Let's take a step back to see if it helps your understanding.

If you have a method that takes a string and an int like this

string MyMethod(string str, int num)
{
    // Do something
}

You'd use it like this

string rslt = MyMethod("Hello", 123);

What you've got here is something that takes in some stuff, does something to it, and gives you something in return. In this case MyMethod takes a string and an int, does something with them and returns a string which you then call rslt.

Your example follows the same basic pattern so you need to take your method - orderArray and give it the two things it wants - an int array and an option like this

int[] arr = new int[] { 3, 8, 0, 2, 16 };

orderArray(arr, options.UpSortOption);

Alternatively, you could create an options much like you'd create a string and then call your method like this

int[] arr = new int[] { 3, 8, 0, 2, 16 };
options myOption = options.UpSortOption;

orderArray(arr, myOption);

To fully illustrate the point that an enum as a parameter isn't any different from say a string you could modify your method like this

static void orderArray(int[] array, string op)
{
    if (op == "UpSortOption")
    {
        Array.Sort(array);
        foreach (int number in array)
        {
            Console.Write(number + " ");
        }
    }
    else
    {
        Array.Sort(array);
        Array.Reverse(array);
        foreach (int number in array)
        {
            Console.Write(number + " ");
        }
    }
}

Then call it like this

int[] arr = new int[] { 3, 8, 0, 2, 16 };
string myOption = "UpSortOption";

orderArray(arr, myOption);

Upvotes: 2

Dominik Szymański
Dominik Szymański

Reputation: 822

Exactly that's how you set up Enum as a method parameter.

To call this method use:

orderArray(arr, options.UpSortOption);

You can also assign enum value to a variable and use this to call a method:

options sortOpt = options.UpSortOption;
orderArray(arr, sortOpt);

You can also cast integer to an enum:

orderArray(arr, (options)0);

OR

int opt = 0;
orderArray(arr, (options)opt);

Remembering, that if not specified otherwise, first element is 0, second is 1 and so on.

By the way, you should rather use PascalCase to name an enum type, like:

public enum Options
{ ... }

Upvotes: 0

fluffy
fluffy

Reputation: 223

This is how you pass it as a parameter

orderArray(int [] array, typeof(options)){
    //Beep bap boop
}

Hope this aids you.

Upvotes: 0

Related Questions