Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:58:26+00:00 2026-05-30T21:58:26+00:00

I’m trying to solve a simple parsing problem and I elected to use enums

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:58:27+00:00Added an answer on May 30, 2026 at 9:58 pm

    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.
    • You can’t cast to a type known only at runtime. In other words, code like (foo.GetType())bar will never work.
    • You can’t use the 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.
    • The names of types (including 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.