2

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;

        }
    }
}
4
  • 1
    orderArray(arr, options.UpSortOption) or you can pass options.DownSortOption. Enum values are retrieved via enum type name. Note that by convention type names should be in PascalCase. I would recommend you to use name like SortDirection and enum members Up and Down (or Ascending and Descending) Commented Jun 6, 2017 at 10:20
  • Visual studio supports you, just enter orderArray, add your array as first parameter and wait until vs-intellisense shows you the available options values as second argument. Commented Jun 6, 2017 at 10:22
  • The method is perfectly fine. What exactly the issue are you facing during method call? Commented Jun 6, 2017 at 10:39
  • Thank you, guys. I just did not know that enum values should be got via enum type. Now I know... Commented Jun 6, 2017 at 11:00

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for another option to set enum as parameter
@anna: Happy to help you always, You can mark the answer as accepted if it helps you to solve the issue as well as helps to learn something
2

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

1 Comment

Got it! Thanks for detailed explanation.
0

This is how you pass it as a parameter

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

Hope this aids you.

4 Comments

That's not what anna is asking for.
"How to set enum as method parameter?" That is the question is it not?
Have you read the question or just the title? An enum value is needed, not the type.
I am trying to give advice to her way of solving the problem. Not giving her a different way that works for me. If she wishes to pursue this problem her way with regards to parsing enum as a parameter then i will give my opinion
0

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
{ ... }

1 Comment

Thanks, now I know one more way to get enum via its value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.