Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

I have this function as shown below:

Private Function getDimensions(ByVal bigMeasurement As String, ByVal smallMeasurement As String) As Int32
       Dim measurement As Double
       Dim strMeasurement As String
       If smallMeasurement = "0" Then
           measurement = Convert.ToInt32(bigMeasurement)
       Else
           strMeasurement = bigMeasurement + "." + smallMeasurement
           measurement = Convert.ToInt32(strMeasurement)
       End If
       Return measurement
   End Function

When it executes I get an error Input string was not in a correct format.

 

Can anyone please help?

 

Thanks, Dave. :D :D :D

Posted

First off, integer type values cannot contain decimals, only floating-points can do that (ie: single, double).

Change the return type of the function to Double, and use Convert.ToDouble() instead of Convert.ToInt32().

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
Note that your method should either accept integers or do some error checking. Also, I'd be inclined to use something like:
        Dim measurement As Double

       If Not Double.TryParse("0" & bigMeasurement & "." & smallMeasurement, _
                              Globalization.NumberStyles.AllowDecimalPoint, _
                              Nothing, _
                              measurement) Then
           MessageBox.Show("The measurements need to be integers you idiot!")
       End If

  • 3 weeks later...
Posted

If you intend to convert a String to an Integer, use the function: "CInt( )"

 

Eg.

 

Assume that a user entered a number '9' into a textbox. Now, that number '9' is a string. If you wish to use the no. '9' for arithmetic calculations; such as add/subtrac/multiply/divide etc, then write this:

 

-----------------------------------------------------------------------------------

 

Dim result As String

 

result = CInt(TextBox1.Text)

' this line converts String '9' to Integer '9' ;assigns the resulting value into "result"

' only when you are using a string (in this case, numbers), for calculation then you'll need to convert into Integer.

 

 

Here are some other conversions:

 

CDbl : Convert to Double

CInt : Convert to Integer

CStr : Convert to String

 

 

-----------------------------------------------------------------------------------

sOMEONE'S gONNA dO iT, wHY nOT yOU ?
Posted
Assume that a user entered a number '9' into a textbox.
This is exactly why I recommended Double.TryParse, because it is probably not safe to assume that the user has entered a number, unless you have used some validation techniques to restrict input to the TextBox. If you make this assumption and the user enters anything that is not an integer then you'll get an unhandled exception. TryParse attempts to convert a string to a number in the format you specify without throwing an exception if it fails.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...