tags:

views:

123

answers:

2

I've encourted a problem recently about cycling between constants of an enum class in .net (that is created from my OWL class, by Rowlex OwlGrinder). Problem was solved by means of using .net reflection (thanks to great help from dear Mr. Jon Skeet): stackoverflow:problem-cycling-enum-class-values

By solving this, I started using it. After matching a dropDownList selected value to one of the enum class instances, I had to declare the selected object(=Language) to my RDF subject(=learningResource), via a predicate (=hasLanguage).

//learningResource is a new RDF subject, hasLanguage is predicate, and there
        //is a new value for it - Language.

        System.Reflection.FieldInfo[] resLanFields =
            typeof(Language).GetFields();

        for (int i = 0; i < resLangFields.Length; i++)
        {
            if (resLanFields[i].Name.Equals(dropDownList_lang.SelectedValue))
                learningResource.hasLanguage = ??? //i-th constant of Language
        }

Now the problem appears; I can not use Language[i] (or something like this to select i-th constant of Language class) to assign to hasLanguage. Is there a way to select i-th constant of an enum class (like refelections)? Would any one please help me in this situation?

A: 

I'd say

resLanFields[i].GetValue(null)

but if this works, don't vote me up, this was in Jon's original answer.

Jimmy
Thanks for answer. It didn't work like that because "learningResource.hasLanguage" just accepts type of "Language" assigning to it.
Ehsan
+1  A: 

The Language class is not an enum in C# terminology. It is an ordinary class with public const string fields. ROWLEX intentionally generates enum-imitating-classes instead of native enums for 2 reasons:

  1. The value of an native C# enum is an integer, while a public const string field can take the URI of the OWL class instance.
  2. The class can have one additional public static string that is "URI" which represents the class URI consistently for every single ROWLEX generated class.

That was the background. If I understood your question right, you had an issue binding the selected name displayed in the dropdown back to the URI, and you wanted to use the position of the element inside the array you created. I would not do it that way. DropDownLists typically have both ID and Value fields for every list item (can be named differently component to component). The ID is expected to be a unique object while the Value is expected to contain something human readable. Set the ID with URI of the "enum" field, and the Value as you have done. So this is how you populate your dropdown:

System.Reflection.FieldInfo[] resLanFields = typeof(Language).GetFields();
foreach(FieldInfo field in resLanFields)
{
  ListItem item = new ListItem();
  item.ID = field.GetValue(null); // takes the URI value of the const field
  item.Value = field.Name; // takes the name of the const field
  dropDownList_lang.AddItem(item);
}

And when the user made his/her choice, this is how you read the result out:

learningResource.hasLanguage = (string) dropDownList_lang.SelectedItem.ID;

Since there is implicit casting operator implemented on the ROWLEX enum-imitating-class (string=>Language) you can safely set your triple subject as a string. Should compile without issue. Simple, isn't it? ;)

ROWLEX Admin
Simple and helpful. Thank you
Ehsan