When to use FOR-CASE (Foreach / switch in C #)?

I found what appears to be the C # equivalent of a FOR-CASE framework in a project I'm working on:

foreach (string param in params.Split(';'))
{
    string[] parts = param.Split('=');
    string key = parts[0].Trim().ToLower();
    string value = parts[1].Trim();
    switch (key)
    {
        case "param1": this.param1 = value; break;
        case "param2": this.param2 = value; break;
        case "param3": this.param3 = value; break;
        case "param4": this.param4 = value; break;
        default: break;
    }
}

      

(Variable names changed to protect the culprit.)

How do I implement this code?

+1


source to share


7 replies


I don't think the code in your question is not similar to the code you linked to.

The code in the question looks like something I could do if I wrote a command line tool.

Am I stupid because I can't see what's wrong with the code in the question?



An alternative is to use reflection to populate the variables of the parameter value. Sometimes I did that too.

BTW: I once wrote a script program that had a switch as the only flow control mechanism and no gosub / return. The code in my program was a bit like the one you linked to. A massive switch to the command pointer variable variable that was reassigned at the end of each case, and an almost endless loop around the switch. He did his job.

+6


source


I see that you already have several fields in your class that you are using to store variables. In this case, what you are doing is fine.

Otherwise, you might have 1 HashTable (perhaps add to C # indexor as a twist) to store all of them and your loop will end up like this:

foreach (string param in params.Split(';'))
{
    string[] parts = param.Split('=');
    string key = parts[0].Trim().ToLower();
    string value = parts[1].Trim();
    MyHashTable[key] = value;
}

      



The problem with this approach is that you should only have 1 value type. For example, if your parameter list can contain both string and int types, it makes the code messier, especially you need to do error checking and checking and stuff.

I personally stick with what you already have.

+4


source


You can use reflection for this:

Type t = this.GetType();
foreach (string param in params.Split(';'))
{    
    string[] parts = param.Split('=');    
    string key = parts[0].Trim().ToLower();    
    string value = parts[1].Trim();    

    t.GetProperty(key).SetValue(this, value, null);
}

      

+3


source


For what it's worth, the WTF article was WTF because its outer loop was completely useless, as noted in the article - it was just as simple and straightforward, just setting the index variable directly than looping and testing it.

+2


source


Not sure if I understand, but it looks like you are complicating yourself. Don't reinvent the wheel, use BCL classes as much as possible, these classes are proven to be effective and save a lot of time. It looks like you could have implemented it with some sort of Dictionary <,> along with, as Guge suggested, Reflection.

+1


source


I really think the OP's code is ok. It's not ideal - there may be simpler or cleaner ways to do this, but it effectively allows you to read mappings between element / property names and props names. It leaves your properties strongly typed (as opposed to hashmap / dictionary solutions, unless your class only has one type for all of its properties ...) and gives you an obvious enough place to fix or add mappings.

0


source


Or Regex:

string parms = "param1=1;param2=2;param3=3";
string[] parmArr = parms.Split(';');        

string parm1 = Regex.Replace(parmArr[0], "param1=", "");
string parm2 = Regex.Replace(parmArr[1], "param2=", "");
string parm3 = Regex.Replace(parmArr[2], "param3=", "");

      

-3


source







All Articles