Using variables in c# switch statement

Dear Experts,

I'm trying to use a variable in a c# switch statement to no avail.

I'm trying to check for cardRef_1, cardRef_2 etc.

Could anybody shed any light on how to do this?

Thanks

Nick


switch (walletArray[0])
                    {
                        case "cardRef_" + cardIndex:
                            w.carfRef = walletArray[4];
                            break;
                      }

Open in new window

LVL 1
nkewneyAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do better:
WalletCard w = new WalletCard();
 
                    switch (walletArray[0].subString(0, walletArray[0].IndexOf("_")) )
                    {
 
                        case "cardRef_":
                            w.cardRef = walletArray[1];
                            break;
                        case "cardNumber_":
                            w.cardRef = walletArray[1];
                            break;
                        case "cardholderName_":
                            w.cardRef = walletArray[1];

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this:
switch (walletArray[0].subString(8,100))
                    {
                        case cardIndex:
                            w.carfRef = walletArray[4];
                            break;
                      }

Open in new window

0
 
nkewneyAuthor Commented:
Hi angelIII,

I have to check for various options so not sure this would work.... What do you think?

 
WalletCard w = new WalletCard();
 
                    switch (walletArray[0])
                    {
 
                        case "cardRef_":
                            w.cardRef = walletArray[1];
                            break;
                        case "cardNumber_":
                            w.cardRef = walletArray[1];
                            break;
                        case "cardholderName_":
                            w.cardRef = walletArray[1];

Open in new window

0
 
williamcampbellCommented:
Maybe use if instead
            if ( ( walletArray[0].IndexOf ("cardref_" + cardIndex ) != - 1 ) ) ||
                 ( walletArray[0].IndexOf ("cardNumber_" + cardIndex ) != - 1 ) ) ||
                 ( walletArray[0].IndexOf ("cardholderName_" + cardIndex ) != - 1 )
            {
                w.cardref = wallearray[l];
            }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.