Hello,
i've just started with vb.net, and when i run this little thingy i created i get the following error:
[italic]"A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll"[/italic]
The program works fine though.
I've read a bunch of articles about this error, and it turns out it's not that uncommon ([link=
http://blogs.msdn.com/davidklinems/archive/2005/07/12/438061.aspx]link[/link]), but I'd really like to know what's causing it and if I can prevent it.
Here's my code
form1 code:
[code]Public Class Form1
Private gobank As Bankrekening
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Initialize()
End Sub
Private Sub Initialize()
gobank = New Bankrekening()
TextBox1.Text = CInt(0)
Label3.Text = CInt(0)
Label4.Visible = False
Label7.Visible = False
Label8.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rekeningsaldo As Integer
rekeningsaldo = TextBox1.Text
If RadioButton1.Checked = True Then
gobank.Storting(rekeningsaldo)
Label6.Text = "(+) Je hebt zonet " & CStr(rekeningsaldo) & "
Comments
Without more information, my first gut feeling is that this line is crashing:
rekeningsaldo = TextBox1.Text
Rekeningsaldo is an Integer, and you are trying to set an Integer equal to a String (TextBox1.Text). If the String value is not a number, then you will get an InvalidCastException and the software will crash... There are a few different solutions, none better than the other, but here is what I would do, replace your Button1_Click code with this:
[code]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rekeningsaldo As Integer
If IsNumeric(TextBox1.Text.Trim()) Then '.Trim removes spaces from the beginning and end of the string
rekeningsaldo = CInt(TextBox1.Text.Trim())
If RadioButton1.Checked = True Then
gobank.Storting(rekeningsaldo)
Label6.Text = "(+) Je hebt zonet " & CStr(rekeningsaldo) & "