Let’s say I have an enum called eEventID that contains 10 events, mEvent1 - mEvent10 with int values 10001 – 10010 . I want to use an input value (e.g. 10007) to check if a member in the enum has the corresponding value. Once I find the event with the corresponding input value, I then take that event and register a method to it by hooking up a delegate (This is assuming I have all the necessary code). I just need to know if a member in the enum has a value that is the same as the input value, then register a new method to that event. How do I look for a certain event using only an input value? I’m guessing a for-loop or any other loop isn’t my best bet.
EDIT: this is what I have so far…
public delegate void EventDel(int mEvtIdx);
public enum eVtEvtId
{
Event1,
Event2,
Event3,
Event4,
Event5,
Event6,
Event7,
Event8,
Event9,
Event10,
}
public void Subscribe(int mInVal)
{
eVtEvtID mEventID;
int mEventIndex = mInVal;
if(Enum.IsDefined(typeof(mEventID), mEventIndex))
{
mEventID += EventDelegate([insert method here])
}
else
{
// will warn the user that the event does
// not yet exist in the enum
}
}
Here’s an example that demonstrates how you would take a value (reading it from the console) map it to the numeric value corresponding to an enum member, locate the corresponding event (in this case on the
Programclass) and attach an event handler to it.