Zeelia


Hi!

I wrote this code:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

If txtUserName.Text = "Test" & txtPassword.Text = "Test" Then

Me.Close()

Else

Form2.Show()

MsgBox("Wrong username or password." & vbCrLf & "Protection Continued")

End If

End Sub

And when i try to run it (debug) i get InnerCastExeption error.

How can i fix this

//Greetings, Zeelia



Re: InnerCastExeption error.

kleinma


First of all, its an InvalidCastException, not an InnerCastException.

It means you are casting one data type to another that can not be directly casted.

It is clear you are coding with OPTION STRICT OFF, which is bad in my opinion.

You can do a search and read about pros and cons about option strict, but the bottom line is if you had it on, you would have gotten a syntax error and not even been able to run your program until you fixed it.

The syntax error is here in this line:

Code Block
If txtUserName.Text = "Test" & txtPassword.Text = "Test" Then

The & operator is used to join strings like

Code Block
Dim MyString as String = "HELLO " & "WORLD"

What you want to use is the AND keyword

Code Block
If txtUserName.Text = "Test" and txtPassword.Text = "Test" then

you can take this one step further and use AndAlso (if you are using .NET 2.0 or higher)

Code Block
If txtUserName.Text = "Test" AndAlso txtPassword.Text = "Test" then

which works the same way as And, but doesn't bother to even look at txtPassword.Text unless txtUserName.Text matched your check and evaulated to true.

Again, I highly recommend you turn option strict on for your coding.