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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:46:27+00:00 2026-05-12T18:46:27+00:00

Alright, i dont know how to explain it well.. but i have a switch

  • 0

Alright, i dont know how to explain it well.. but i have a switch statement,

string mystring = "hello";
switch(mystring)
{
case "hello":
break;
case "goodbye":
break;
case "example":
break;
}

of course this is an example, and in the real situation, there will be different things happening for each case.
ok, hope you get the point, now, doing this manually is impossible, because of the sheer number of different case’s. i need to respectively create a list, of all the cases, so for instance.. for the above switch statement, i would need

string[] list = { "hello", "goodbye", "example" };

maybe could be done with a foreach some how i dont know, any help would be greatly appreciated.

also, any working codes provided would be awesome!

edit:
people are asking for more detail, so here is how it works.
the user of the program, inputs a series of strings.
based on the string(s) they entered, it will do a few if’s and else if’s and throw back the new strings basically. i need to be able to be able to create a list, through the program, of all the options available to use. and i cant just make a list and hard code it in, because im always adding more case’s to the statement, and i cant be going back and keeping a list up to date.

  • 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-12T18:46:27+00:00Added an answer on May 12, 2026 at 6:46 pm

    It depends on how clever you want to get… You could create a custom attribute that attaches to a method with the string that method should handle. Then, instead of a switch statement, you would just find the attribute with your desired value and execute it.

    using System;
    using System.Reflection;
    
    namespace ConsoleApplication1 {
        [AttributeUsage(AttributeTargets.Method)]
        internal class ProvidesAttribute : Attribute {
            private String[] _strings;
            public ProvidesAttribute(params String[] strings) {
                _strings = strings;
            }
            public bool Contains(String str) {
                foreach (String test in _strings) {
                    if (test.Equals(str)) {
                        return true;
                    }
                }
                return false;
            }
        }
    
        internal class Program {
            [Provides("hello", "goodbye")]
            public void HandleSomeStuff(String str) {
                Console.WriteLine("some stuff: {0}", str);
            }
    
            [Provides("this")]
            public void HandleMoreStuff(String str) {
                Console.WriteLine("more stuff: {0}", str);
            }
    
            public void HandleString(String str) {
                // we could loop through each Type in the assembly here instead of just looking at the
                // methods of Program; this would allow us to push our "providers" out to other classes
                MethodInfo[] methods = typeof(Program).GetMethods();
                foreach (MethodInfo method in methods) {
                    Attribute attr = Attribute.GetCustomAttribute(method, typeof(ProvidesAttribute));
                    ProvidesAttribute prov = attr as ProvidesAttribute;
                    if ((prov != null) && (prov.Contains(str))) {
                        method.Invoke(this, new Object[] { str } );
                        break;  // removing this enables multiple "providers"
                    }
                }
            }
    
            internal static void Main(String[] args) {
                Program prog = new Program();
                foreach (String str in args) {
                    prog.HandleString(str);
                }
            }
        }
    }
    

    Once you have the framework, you wouldn’t need to alter the HandleString() code, just add the methods you want to take care of and set the Provides attribute on them. If you wanted to extend the idea a little further, you could create multiple classes to handle a wide variety of strings, then loop through each type in your assembly looking for the Provides attribute.

    EDIT this has the added benefit that you can define multiple methods that act on the same string (by removing the break in the loop logic).

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

Sidebar

Related Questions

Alright, I know my title is a little obscure but it best describes the
Alright, currently I have my SWF hitting a php file that will go and
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, I know how the fieldset / legend works out in HTML. Say you
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright let me explain my situation first: I am part of an organization that
Alright so I have this C++ image capturing class. I was wondering if I
Alright so im done doing pages with frames, and know i splitted up my

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.