An even safer approach might be to use the SelectedValue property, which is the same as the SelectedItem.Value property along with the String.IsNullOrEmpty() method to check for empty or null values :
You didn't need to previously include all of the Convert.ToString() methods and other .ToString() method calls as
the SelectedValue property is already a string, so you don't need to worry about attempting to convert it again.
if too many sentinels gets added kinda cryptic to read. I would do
using System.Linq;
var invalidSelections = new[]{null, string.Empty, "0"}; //add other invalid inputs here as per need
if(!invalidSelections.Contains(a))
{
//do stuffs
}
Member
54 Points
123 Posts
if loop otmization
Aug 14, 2014 09:55 AM|krishna2099|LINK
please have a look at following simple code
if (usercmb.SelectedItem.Value != "" )
{
if (usercmb.SelectedItem.Value != "0")
{
cnt2.UserName = Convert.ToString(usercmb.SelectedItem.Value.ToString());
}
}
i want to merge both the first lines logic i.e can i do like this
if(usercmb.SelectedItem.Value!=="" || usercmb.SelectedItem.Value!==0)
please help
All-Star
114593 Points
18503 Posts
MVP
Re: if loop otmization
Aug 14, 2014 10:01 AM|Rion Williams|LINK
You could absolutely do that :
An even safer approach might be to use the SelectedValue property, which is the same as the SelectedItem.Value property along with the String.IsNullOrEmpty() method to check for empty or null values :
And just for fun a one liner :
You didn't need to previously include all of the Convert.ToString() methods and other .ToString() method calls as the SelectedValue property is already a string, so you don't need to worry about attempting to convert it again.
Participant
1731 Points
1146 Posts
Re: if loop otmization
Aug 14, 2014 10:03 AM|karang|LINK
try this
Karan Gupta
http://gyansangrah.com
Please click "Mark as Answer" if this helped you.
All-Star
101931 Points
20703 Posts
Re: if loop otmization
Aug 14, 2014 10:10 AM|MetalAsp.Net|LINK
I would write it like this:
Member
368 Points
197 Posts
Re: if loop otmization
Aug 15, 2014 04:13 PM|ossprologix|LINK
if too many sentinels gets added kinda cryptic to read. I would do