I'm trying to solve a simple parsing problem and I elected to use enums to encode lists of choices.
The input data is straight ascii text, broken into blocks with unique header and non-unique identifiers where the data is. I'm able to write pretty general symbolizing methods without providing any context as to the data meaning and deal with it once it's returned.
Doing this with strings is no problem. I just pass a List in and away we go.
I can't figure out the syntax to generalize an enum, and I could use some help. I could also be far too locked into imperative thinking and missing an easy answer to this.
Here's the code I'm having a hard time with
private void parseToEnums(Enum returnEnum, string searchBlock, string startIDText,
string endIDText, string startText, string endText)
{
string ourSearchBlock = searchBlock;
int endIDidx = ourSearchBlock.IndexOf(endIDText);
while (ourSearchBlock.IndexOf(startText) != -1)
{
if (ourSearchBlock.Length == searchBlock.Length)
{
// first pass, trim off the region where the start text isn't valid
ourSearchBlock = ourSearchBlock.Remove(endIDidx, ourSearchBlock.Length - endIDidx);
// first pass, use the startIDtext to create a valid search zone
// BROKEN CODE HERE
// Neither GetType() nor typeof seem to do the right thing
// I have tried several varieties and have tried casting the LHS in the
// same sort of way
// pluckText returns a string that is guaranteed to match an enum name
returnEnum = (returnEnum.GetType()) System.Enum.Parse(typeof(returnEnum), pluckText(ourSearchBlock, startIDText, startText, endText), false);
ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startIDText) + startIDText.Length);
}
else
{
// this would be similar to the above after it's working
// and is for the case where the string has multiple matches
// within the enum, ie "red white"
//returnList.Add(pluckText(ourSearchBlock, "", startText, endText));
}
ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startText) + startText.Length);
}
return;
}
Example of what I'm doing
private enum Colors { red, white, green };
private enum Suits { spades, clubs, hearts, diamonds };
// ... open files, read data, etc
// so I pass in the enum that I want my result in and some text identifiers
parseToEnum ( Colors, searchBlock, "startColorBlock", "endColorBlock", "id=" );
parseToEnum ( Suits, searchBlock, "startCardSuitsBlock", "endCardSuitsBlock", "<id=" );
// ...
So the idea is to use the same structure (since the inputs are the same) but use different enums for the output.
I am aware that I need to add some try/catch wrappers and general error detection into this code before too much longer.
I'm going to ignore all that searching and focus on converting a string to an enum.
First, I think your method should return the result, not pass it as a parameter (you would need out for that).
Second, to pass the type of the enum to the method, you can either use a a parameter of type Type, or, even better, make the method generic and pass the type as a type argument.
The method could look like this:
T ParseEnum<T>(string s)
{
return (T)Enum.Parse(typeof(T), s, false);
}
You could then call it like this:
Colors color = ParseEnum<Colors>(someString);
The errors in your code are:
Enum is a common base type of all enums, it does not represent the type of an enum. That means you can't use for example Colors as a parameter to a method.(foo.GetType())bar will never work.typeof operator to get the type of a variable. You can use it to get the Type object for some specific type, e.g. typeof(string) or typeof(T) in a generic method with type argument T.enums) should be in singular. That's because for example variable of type Color represents one color. Though this is only a style issue and it won't stop your code from working. But it will make your code harder to understand.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With