The Parse method will throw an ArgumentNullException if the value being
passed is nothing. You can use a Try/Catch to catch the
ArgumentNullException. I'm not a C# programmer, but in VB it would look like:
dim myVariable as integer
Try
myVariable = Int32.Parse(txtControl1.Text.Trim())
'If I get here then all is well. Do something with the number.
myVariable = myVariable * 5
Catch exane as ArgumentNullException
'Value is null; do something else:
lblError.Text = exane.Message
Catch ex as Exception
'To catch all other errors
lblError.Text = ex.Message
End try
Hope this helps.
if (myVariable != null) {// do something}
"js" wrote:
I need to test if an integer variable is null or not. I tried to do
int myVairable;
myVariable = Int32.Parse(txtControl1.Text.Trim());
if (myVariable != null) {// do something}
C# does not allow this statement. How do I test an integer to see if it
is null or not? Thanks.