C# - working with enumerators.

Hi Experts,

           I am a new bie to c#. i have created a class file with enumerators.
I want to write a function in my codebehind so that, i get the values from enumerator where
the names starts with "WB_ALL" and then check their values against the values
of user input. If the values from user matches, then perform something.[which i will take care of.]

Please help me on writting that function...
Class file - sample.cs
_______________________
namespace UDP_Workout
{
    class Sample
    {
        public enum PacketIdentifierEnum32 : uint
        {
            WB_ALL_DISPATCH_EXERCISE = 301,
            WB_ALL_DISPATCH_PLAYBACK = 302,
            WB_ALL_DISPATCH_C2_REPORT_MODE = 303,
            WB_ALL_STOP_EXERCISE = 304,
            WB_ONE_DISPATCH_EXERCISE = 305,
            WB_ONE_DISPATCH_PLAYBACK = 306,
            WB_ONE_DISPATCH_C2_REPORT_MODE = 307,
            WB_ONE_STOP_EXERCISE = 308,
        }
    }
}
________________________________________________________

Open in new window

srk1982Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag AgarwalPython DeveloperCommented:
Use switch case in your function as mentioned in the below code.
 
Anurag

namespace UDP_Workout
{
    class Sample
    {
        public enum PacketIdentifierEnum32 : uint
        {
            WB_ALL_DISPATCH_EXERCISE = 301,
            WB_ALL_DISPATCH_PLAYBACK = 302,
            WB_ALL_DISPATCH_C2_REPORT_MODE = 303,
            WB_ALL_STOP_EXERCISE = 304,
            WB_ONE_DISPATCH_EXERCISE = 305,
            WB_ONE_DISPATCH_PLAYBACK = 306,
            WB_ONE_DISPATCH_C2_REPORT_MODE = 307,
            WB_ONE_STOP_EXERCISE = 308,
        }
        public function void CheckValues()
        {
           PacketIdentifierEnum32 myEnum = new PacketIdentifierEnum32();
Switch (x)
{
     case myEnum.WB_ALL_DISPATCH_EXERCISE:
         //perform your code
        break;
     case myEnum.WB_ALL_DISPATCH_PLAYBACK:
         //perform your code
        break;
 case myEnum.WB_ALL_DISPATCH_C2_REPORT_MODE:
         //perform your code
        break;
default 
 //do something if nothing matches
 
}
         }
 
 
    }
}

Open in new window

0
srk1982Author Commented:
Hi Anurag,

 Thanks for your response.
 The enumerators will be in class file and the CheckValues()
 will be in code behind. The function shud be generic, so that
 if any enumerators are added in future, the code shud work
 for that too. Just trying to say that, we cannot hardcore the
 enum names in the codebehind. Thats why i want to check
 the names with "WB_ALL" prefix which is not going to change.

Thanks.
0
AruspexCommented:
Hi srk1982,

try this;
foreach (string name in System.Enum.GetNames(typeof(PacketIdentifierEnum32 )))
{
    if (name.StartsWith("WB_ALL"))
    {
    //do stuff
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.