Assume that s is a string variable that is supposed to contain a value to be converted to integer.

Assume that s is a string variable that is supposed to contain a value to be converted to integer.

LANGUAGE: Visual Basic

CHALLENGE:

Assume that s is a string variable that is supposed to contain a value to be converted to integer.  Write a fragment of code that converts the value to integer variable and displays that value multiplied by 2.  If the value cannot be converted, display the message ‘Invalid number’ instead.

SOLUTION:

Try
 MessageBox.Show(CInt(s) * 2)
Catch
 MessageBox.Show("Invalid number")
End Try

Add comment