davearia Posted July 20, 2005 Posted July 20, 2005 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 Quote
IngisKahn Posted July 20, 2005 Posted July 20, 2005 Convert.ToDouble(...) :) Quote "Who is John Galt?"
BlackStone Posted July 20, 2005 Posted July 20, 2005 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(). Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
davearia Posted July 20, 2005 Author Posted July 20, 2005 I apologise for not seeing this myself. But many thanks anyways :D :D :D Quote
jmcilhinney Posted July 22, 2005 Posted July 22, 2005 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 Quote
FYRe Posted August 11, 2005 Posted August 11, 2005 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 ----------------------------------------------------------------------------------- Quote sOMEONE'S gONNA dO iT, wHY nOT yOU ?
jmcilhinney Posted August 11, 2005 Posted August 11, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.