Mattg1981 Posted October 29, 2002 Posted October 29, 2002 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! Quote
Mattg1981 Posted October 29, 2002 Author Posted October 29, 2002 It was in a TextBox and not a InputBox if that makes any difference Quote
reboot Posted October 29, 2002 Posted October 29, 2002 .Net doesn't do casting for you like previous versions. Quote
*Gurus* divil Posted October 30, 2002 *Gurus* Posted October 30, 2002 Show us the code, and show us what error you get. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Mattg1981 Posted October 30, 2002 Author Posted October 30, 2002 (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 November 20, 2002 by divil Quote
Mattg1981 Posted October 30, 2002 Author Posted October 30, 2002 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. Quote
Mattg1981 Posted October 30, 2002 Author Posted October 30, 2002 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. Quote
*Gurus* Derek Stone Posted October 30, 2002 *Gurus* Posted October 30, 2002 TextBox1.Text = Result.ToString() Quote Posting Guidelines
Mattg1981 Posted October 30, 2002 Author Posted October 30, 2002 ack! didnt mean to hit the reload button :( Quote
Mattg1981 Posted October 30, 2002 Author Posted October 30, 2002 Im sorry, I dont understand where to insert that. ( sorry :( ) Quote
*Gurus* divil Posted October 30, 2002 *Gurus* Posted October 30, 2002 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) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Mattg1981 Posted October 31, 2002 Author Posted October 31, 2002 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. 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.