[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using variables in c# switch statement

Posted on 2009-04-28
5
Medium Priority
?
650 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:nkewney
  • 2
4 Comments
 
LVL 143

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:nkewney
ID: 24255516
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 24255548
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
 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 1000 total points
ID: 24255584
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question