Specified cast is not valid

R

RW

Hello,

I have declared a Public Class MDIMenu that Inherits from
System.Windows.Forms.Form.

In the InitializeComponent Sub I declare it as a MdiContainer with this
statement: Me.IsMdiContainer = True

In my Sub Main I start my application by calling an instance of the MDIMenu
class in this way:

Dim frm As New MDIMenu
Application.Run(frm)

I get this error: Specified cast is not valid

When I build or rebuild my solution I don't get no build error's and I can't
trace the error further than the command Application.Run(frm).

So can anyone give me a suggestion what could have gone wrong here?

I already tried to make an other, simpler form to start with, but I get the
same error.
 
M

Mr. Arnold

RW said:
Hello,

I have declared a Public Class MDIMenu that Inherits from
System.Windows.Forms.Form.

In the InitializeComponent Sub I declare it as a MdiContainer with this
statement: Me.IsMdiContainer = True

In my Sub Main I start my application by calling an instance of the
MDIMenu
class in this way:

Dim frm As New MDIMenu
Application.Run(frm)

I get this error: Specified cast is not valid

When I build or rebuild my solution I don't get no build error's and I
can't
trace the error further than the command Application.Run(frm).

So can anyone give me a suggestion what could have gone wrong here?

I already tried to make an other, simpler form to start with, but I get
the
same error.

You do know what *cast* means don't you? You can't cast a Windows.Control
(MDIMenu) to a System.Windows.Form. which is what Application.Run() wants is
a form.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.run(VS.71).aspx
 
R

RW

Thanks Mr. Arnold,

yes, I know what casting is.

and I have declared the Public Class MDIMenu so that it inherits from
System.Windows.Forms.Form, so my frm is a form.

but even when I do it like this:

Dim frm As System.Windows.Forms.Form = New MDIMenu
Application.Run(frm)

still gives the same error

I even tried it with a regular form, not an inherited class, and it doesn't
work.

btw Option Strict is On
 
D

dlm@bypsoft

Hi,

your code is OK and I would said that your error is somewhere else. Can remember that we had similar problems, but in another thread (splash window) and application reported invalid cast from that thread. It would be very interesting for me to see whole Main static function if possible.

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com
 
D

dlm@bypsoft

Hi,

your code is OK and I would said that your error is somewhere else. Can remember that we had similar problems, but in another thread (splash window) and application reported invalid cast from that thread. It would be very interesting for me to see whole Main static function if possible.

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com
 
R

RW

Hey thanks,

The complete code is this:


Public Sub Main()

Try
m_CurrentUser = New ApplicationUser
m_CurrentUser.NTLogin =
System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(System.Security.Principal.WindowsIdentity.GetCurrent().Name.IndexOf("\")
+ 1).ToUpper()

Dim p As New SQLParameter("@NTLogin", m_CurrentUser.NTLogin)
Dim pl(0) As SQLParameter
pl(0) = p
Dim exm As New ExchangeManager
Dim ds As DataSet = exm.GetDataSet("SQLStatements", "GetUser", pl)

For Each dr As DataRow In ds.Tables(0).Rows

With dr
If CType(.Item("IsActive"), Boolean) Then
m_CurrentUser.PK_Gebruiker = CType(.Item("PK_User"),
Guid)
m_CurrentUser.UserFirstName =
..Item("FirstName").ToString
m_CurrentUser.UserLastname =
..Item("Lastname").ToString
m_CurrentUser.UserEmail = .Item("EMail").ToString
Else
Throw New ApplicationException("User " &
..Item("FirstName").ToString & " " & .Item("Lastname").ToString & " is
inactive!")
End If
End With
Next

If m_CurrentUser.UserEmail = System.String.Empty Then
Throw New ApplicationException(m_CurrentUser.NTLogin & " is
not a registred user!")
End If

Dim frm As New MDIMenu
Application.Run(frm)
frm.Text = "..."


Catch ex As System.Exception
If MessageBox.Show(ex.Message, "...", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation) = DialogResult.OK Then
Application.Exit()
End If

End Try


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top