Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
2.43/5 (4 votes)
See more:
can u observe my code below it' so lengthy.. I want write in Shorter way , help me any one know


C#
int paymentType = 0;
            if (lblPayment.Text == "Credit")
            {
                paymentType = 1;
            }
            else if (lblPayment.Text == "Open Invoice")
            {
                paymentType = 2;
            }

            else if (lblPayment.Text == "COD")
            {
                paymentType = 3;
            }

            else if (lblPayment.Text == "Check")
            {
                paymentType = 4;
            }

            else if (lblPayment.Text == "Paypal")
            {
                paymentType = 5;
            }

            else if (lblPayment.Text == "Money Order")
            {
                paymentType = 6;
            }

            else if (lblPayment.Text == "Other")
            {
                paymentType = 7;
            }
Posted

Instead of dealing with strings, you could use an enum. That way you can directly assign value to paymentType.

C#
paymentType = (int)Enum.Parse(typeof(YourEnum),"ValueHere");



Based on what controls you are using on screen, you could bind it to these enum values. Better still, you and use set of RadioButton. Assign Tag property of all radio buttons and have a single CheckedChanged event handler for all. In the handler, you can use something like this:

int paymentType = (int)(sender as RadioButton).Tag;
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 5:18am    
Good idea, +5!
As mentioned - the more right way would be to use a switch-case-statement.


C#
switch(lblPayment.Text)
{
   case "Credit" :
         //paste your action
         break;
}


You could use a dict, but not like the way described above, as translation list for a string.
Use a global static list (enum-type or dict) as source of all payment method related controls.
 
Share this answer
 
Instead of if statement use switch[^] statement.

You can build your own dictionary[^] too.

C#
Dictionary<int,string> payTypes = new Dictionary<int,string>();

// Add some elements to the dictionary. There are no  
// duplicate keys, but some of the values are duplicates.
payTypes.Add(1, "Credit");
payTypes.Add(2, "Open Invoice");
payTypes.Add(3, "COD");
payTypes.Add(4, "Check");
//and so on...


Dictionary class exposes several methods, for example Contains()[^], to quickly find searched value ;)
 
Share this answer
 
v2
Comments
phil.o 3-Mar-14 5:04am    
Hi Maciej, maybe I missed something, but shouldn't it be a Dictionnary<string,int> that is needed here?
In this case, one should read payTypes.Add("Credit", 1);; thus the if blocks could be reduced to paymentType = payTypes[lblPayment.Text].
Maciej Los 3-Mar-14 5:08am    
Phil, thank you for your comments. Yes, maybe you're right. It depends on OP's needs.
Have a look at my answer one more time. Some bugs have been removed.
phil.o 3-Mar-14 5:12am    
Ok, I was just wondering :)
By the way, I had the same "bug" with the second type in the dictionary declaration: I had to edit my comment so that it appears (it seems the '>' replacement with '>' also replaces the second type in the dictionary declaration).
Bajid Khan 3-Mar-14 5:21am    
Thanq very much , its helpful to me
Maciej Los 3-Mar-14 5:22am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900