Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Okay, I just got VS .NET and I am learning it very quickly, but I encountered a small problem along the way. I was writing a program and declared my variable "as double" .. but when I typed a - (negative) in front of the number, I got a runtime error. I then went back and tried "as long" and got the same runtime error. Can anyone explain to me the reason for this runtime error, and how I could correctly declare it in order to type in negative numbers. Thanks!
Posted (edited)

Here is a simplified version of my code that produces the same error:

 

Imports System.Math
Public Class Form1
   Inherits System.Windows.Forms.Form

" Windows Form Designer generated code "

   Public Input As Double

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim Result As Double
       Result = Sqrt(Input)
       TextBox1.Text = Result
   End Sub

   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       Input = TextBox1.Text
   End Sub
End Class

 

ERROR:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

 

Additional information: Cast from string "-" to type 'Double' is not valid.

Edited by divil
Posted
I think I can interpret this problem .. it is saying that Input is a Double .. and when I hit the - button, the - is a string. So do I just declare Input as a string? (I will try that now) but if not, I dont understand how I would type in a neg. number.
Posted
I think I can interpret this problem .. it is saying that Input is a Double .. and when I hit the - button, the - is a string. So do I just declare Input as a string? (I will try that now) but if not, I dont understand how I would type in a neg. number.
  • *Gurus*
Posted

Where you have Input = TextBox1.Text...

 

Just think about it. You're trying to stick a string in to a double, that's just not going to work. You need a method to convert a string in to a double, you can use the VB CDbl function, or, better yet, the Double.Parse method.

 

Input = Double.Parse(TextBox1.Text)

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

LOL .. I am sorry, I know I am making this *WAY* to complicated now ... but doing exactly what you told me divil brought up a new error.

 

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

 

Additional information: Input string was not in a correct format.

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...