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 477793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:37:36+00:00 2026-05-13T00:37:36+00:00

say you have to interpret a sting of command arguments like AABBCDEEFF… and the

  • 0

say you have to interpret a sting of command arguments like AABBCDEEFF… and the idea is that each character represents a command for which you have to take some actions on the class, the ugly solution is to write a big switch-case but i dont want to use that , can anybody suggest a more elegant solution ??

  • 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-13T00:37:36+00:00Added an answer on May 13, 2026 at 12:37 am

    For C#, In the more complex case a solution can be to implement and interface, or add attributes and use reflection to call the command or method.

    OK, as an example i created some C# code

    public class ExecuteSequence
        {
            Dictionary<string, Type> classes = new Dictionary<string, Type>();
            public void LoadClasses()
            {
                classes.Clear();
                //load all classes with attribute
                //this can be done at startup once, or on requested refresh
                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                    foreach (Type type in assembly.GetTypes())
                        foreach (object attribute in type.GetCustomAttributes(typeof(MethodAttribute), true))
                            if (attribute.GetType() == typeof(MethodAttribute))
                                classes.Add(((MethodAttribute)attribute).MethodName, type);
            }
    
            public string Execute(string sequence)
            {
                string retVal = "";
                foreach (char c in sequence)
                    if (classes.ContainsKey(c.ToString()))
                    {
                        IMethod method = (IMethod)Activator.CreateInstance(classes[c.ToString()]);
                        retVal += method.Execute();
                    }
    
                return retVal;
            }
        }
    
        public class MethodAttribute : Attribute
        {
            private readonly string m_MethodName;
            public MethodAttribute(string methodName)
            {
                m_MethodName = methodName;
            }
            public string MethodName
            {
                get { return m_MethodName; }
            }
        }
        public interface IMethod
        {
            string Execute();
        }
    
        [Method("A")]
        public class MethodA : IMethod
        {
            public string Execute()
            {
                return "FOO";
            }
        }
    
        [Method("B")]
        public class MethodB : IMethod
        {
            public string Execute()
            {
                return "BAR";
            }
        }
    

    You can limit the list of initial assemblies scanned, but as mentioned, this should only load on startup.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have like this: \\n , I need to convert it
Say I have a float that I want to interpret as an unsigned int
Okay, let's say that I have b.exe, which takes a string argument. I want
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have a method that returns this. Vector[ (PkgLine, Tree) ]() I want
Say I have a macro like this: #define SET_TYPE_NAME(TYPE, NAME) \ template<typename T> \
Note : I have tried using unserialize function but it say that first argument
I have a text file as input like this:let say it as xyz.txt TIME
Say I have an array of chars, which I have allocated on the heap,
Before explain my problem, I have to say that my GWT version is 2.2.0

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.