Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert string to integer..

Status
Not open for further replies.

Vandy02

Programmer
Jan 7, 2003
151
0
0
US
i am trying to covert a string to an integer....
I have tried v_counting = Convert.ToInt32(ENAME.Text);

but I keep getting errors..not in correct format....
 
Try this

string strVariable = "3";

int intvariable = Convert.ToInt16( strVariable );
 
Good point:

string strVariable = "3";
int intVariable = 0;

if ( strVariable == "" )
{
strVariable = "0";
intVariable = Convert.ToInt16( strVariable );
}
else
intVariable = Convert.ToInt16( strVariable );
 
The VB.NET to C# converter Instant C# makes a static class available to mimic the robustness of the VB cast macro "CInt":

internal static int CInt(object oValue)
{
try
{
if (oValue == null || System.Convert.IsDBNull(oValue))
//default null to 0 (identical to the VB.NET cast):
return 0;

double dValue;
if (double.TryParse(oValue.ToString().Trim(),
NumberStyles.Any, CultureInfo.CurrentCulture, out dValue))
if (dValue <= int.MaxValue && dValue >= int.MinValue)
return (int)(System.Math.Round(dValue));
else
return 0;
else
return 0;
}
catch
{
return 0;
}
}
 
could you not do a Int.Parse(string)
 
If you pass in a non-integer compatable string to Int.Parse, it will throw a System.FormatException.

So long as the Parse operation is handled within a try .. catch block then problems can be dealt with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top