friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:18:56

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Retrieve value from an enumeration [c#]

This is a snippet I use to retrieve the integer value from an enumeration


Instructions: Pass the value you're looking for, and the enumeration to search, and it will return the integer value of the enumeration.


Code:

/// <summary>
/// Fnction to return the text value based on the index provided
/// </summary>
/// <param name="_enum">Enum to work with</param>
/// <param name="name">The value we're looking for</param>
/// <returns></returns>
/// <remarks></remarks>
public static string GetStringValue(Type _enum, string name)
{
    string str = string.Empty;
    //get the names from the enumeration
    string[] names = System.Enum.GetNames(_enum);
    //get the values from the enumeration
    int[] values = System.Enum.GetValues(_enum);
    //Now we loop through all the names in the enum list
    //looking for a match
    for (int i = 0; i <= names.Length - 1; i++)
    {
        //If this current index matches the name
        //then we return that name
        if (names[i] == name)
        {
            str = names[i;
            break; 
        }
    }
    return str;
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 0.024 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)