Alan2
Alan2

Reputation: 24572

Is there a way I can get an Enum value by its number in the sequence?

I have this enum:

public enum PTI
{
    UserInput = 0,
    Five = 5,
    Ten = 10,
    Fifteen = 15
}

public static partial class Extensions
{
    public static string Text(this PTI time)
    {
        switch (time)
        {
            case PTI.UserInput: return "User Input";
            case PTI.Ten: return "10 seconds";
            case PTI.Five: return "5 seconds";
            case PTI.Fifteen: return "15 seconds";
        }
        return "";
    }
}

It's a simple case but I am using it as an example. In my code I need to pass the value of the enum to a function so I came up with this:

if (selectedIndex == 1) App.DB.UpdateSetting(Settings.Pti, PTI.UserInput);
if (selectedIndex == 2) App.DB.UpdateSetting(Settings.Pti, PTI.Five);
if (selectedIndex == 3) App.DB.UpdateSetting(Settings.Pti, PTI.Ten);
if (selectedIndex == 4) App.DB.UpdateSetting(Settings.Pti, PTI.Fifteen);

It's not ideal. Maybe a Case would help here but my point is that all I get is a number from 1 to 4 and from this I need to get the ENUM value.

Is there some way that I could get the ENUM value from the value of selectedIndex without an if or case construct?

Background - Here's where the value of selectedIndex comes from. It's from a picker in a Xamarin Form XAML

<Picker x:Name="ptiPicker" IsVisible="false" SelectedIndexChanged="ptiOnPickerSelectedIndexChanged">
                                            <Picker.Items>
                                                <x:String>User input</x:String>
                                                <x:String>5 seconds</x:String>
                                                <x:String>10 seconds</x:String>
                                                <x:String>15 seconds</x:String>
                                            </Picker.Items>
                                        </Picker>

Note:

If it was possible I would be happy to add some extension method to the enum if that would help get what I needed. Just not sure how to do that.

Upvotes: 1

Views: 1471

Answers (4)

bogdanbrizhaty
bogdanbrizhaty

Reputation: 145

I'm not familiar with Xamarin, but may be you should try to fill items with elements of your enum directly? In WPF It goes simply like this. And if you want to convert enum elements to string and back, you can use ValueConverter The example below is from WPF!

        <ComboBox
        // properties here
        SelectedItem="{Binding SelectedItem.EngineType, UpdateSourceTrigger=PropertyChanged}">
        <Model:EEngineType>Diesel</Model:EEngineType>
        <Model:EEngineType>Gas</Model:EEngineType>
        <Model:EEngineType>Biofuel</Model:EEngineType>
        <Model:EEngineType>Electrical</Model:EEngineType>
    </ComboBox>

and EEngineType

public enum EEngineType
{
    Diesel,
    Gas,
    Biofuel,
    Electrical
}

May be you should look around something like this, so you won't need to convert anything and just pass selectedItem to your method.

OR in another way you can try to use something like this

readonly TCollection _PITCollection;
// ctor here
 _PITCollection = new TCollection<PIT> { PIT.UserInput, PIT.Five, PIT... };

Where instead of TCollection you write any collection type you want. And than you can use binding and call

App.DB.UpdateSetting(Settings.Pti, myPicker.SelectedItem);

or you can use you previous style and call

App.DB.UpdateSetting(Settings.Pti, _PITCollection[selectedIndex]);

Hope this will help you

Upvotes: 1

Habeeb
Habeeb

Reputation: 8007

You are trying to get the index of the Enum value.

// Below code sample how to find the index of Enum.
// Sample to show how to find the index of item
PTI p = PTI.Ten; 
int index = Array.IndexOf(Enum.GetValues(p.GetType()), p);
Console.WriteLine(index); // Output: 2 because of value PTI.Ten

So, finally solution for you to replace all if statements with just one statement.

PTI pti = (PTI)(Enum.GetValues(typeof(PTI))).GetValue(selectedIndex-1); // selectedIndex-1 because the index is 0 based.
App.DB.UpdateSetting(Settings.Pti, pti);

Upvotes: 2

Jeric Cruz
Jeric Cruz

Reputation: 1909

You can try to convert it as an integer to get the value.

Convert.ToInt32(PTI.Fifteen)

enter image description here

Picker items are in a form of string so what you can do is to get the text of the selected index of your picker and parse it to only get the number from item.

picker.Items [picker.SelectedIndex];

e.g.

var selectedValue = "15 seconds"; //this will be your selected value in picker
var splitx = selectedValue.Split(' ');

Console.WriteLine(Convert.ToInt32(PTI.Fifteen));

if (Convert.ToInt32(splitx[0]) == Convert.ToInt32(PTI.Fifteen))
{
    Console.WriteLine("selected value is - 15 seconds");
}

enter image description here

You can also try to do a Dictionary like in the Xamarin example here

Hope this helps you...

Upvotes: 0

CodingNagger
CodingNagger

Reputation: 1538

You can cast your int with PTI like in this program

public class Program
{
    public static void Main(string[] args)
    {

        //Your code goes here
        Console.WriteLine(""+(PTI)2);
        Console.WriteLine(""+(PTI)5);
    }
}

public enum PTI
{
    UserInput = 0,
    Five = 5,
    Ten = 10,
    Fifteen = 15
}

If the integer has a valid value existing in the enum you get the enum, if not you will get the integer unchanged as the result of the program above shows below:

2
Five

Upvotes: 1

Related Questions