(ASP.NET / C#) Anyway to froce a cast from string to int?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Programming ASP.NET w/ C#, i have a DropDownList control that when selected,
fires the SelectedItemChanged event. The "values" for this control are
numbers (although stored as strings). For example:

"Green", "100"
"Red", "203"
"Etc.", "545"

Is there any way to force a cast operation to convert these strings "values"
to integers? The SelectedValue property of the drop down control determines
the count for a loop sequence.

If this is not possible, can someone recommend a way to determine which
numerical value is selected?

Thanks,
 
this sort of thing what you're looking for?

int SelectedValue = 0;
try
{
SelectedValue = Convert.ToInt32 ( DropDownList1.SelectedValue )
}
catch ( Exception ex )
{
// not a number presumably
}
 
Yes. Thanks.

Dan Bass said:
this sort of thing what you're looking for?

int SelectedValue = 0;
try
{
SelectedValue = Convert.ToInt32 ( DropDownList1.SelectedValue )
}
catch ( Exception ex )
{
// not a number presumably
}
 
you can use
try{
int.Parse(stringValue);
}catch{}
or to be on safe side use the following code
double tmp;
if(double.TryParse(stringValue, Globalization.NumberStyles.Any,null,out
tmp))
{
xyz = int.parse(stringValue);
//or
xyz = (int)tmp;
}
 
Back
Top