Windows Vista Forums

Specified cast is not valid
  1. #1


    RW Guest

    Specified cast is not valid

    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.


    --
    RW

      My System SpecsSystem Spec

  2. #2


    Mr. Arnold Guest

    Re: Specified cast is not valid


    "RW" <RW@xxxxxx> wrote in message
    news:5CBC9DF7-92CE-4E6F-B78E-FD12A193BD61@xxxxxx

    > 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/lib...un(VS.71).aspx


      My System SpecsSystem Spec

  3. #3


    RW Guest

    Re: Specified cast is not valid

    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


    --
    RW


    "Mr. Arnold" wrote:

    >
    > "RW" <RW@xxxxxx> wrote in message
    > news:5CBC9DF7-92CE-4E6F-B78E-FD12A193BD61@xxxxxx

    > > 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/lib...un(VS.71).aspx
    >
    >

      My System SpecsSystem Spec

  4. #4


    RW Guest

    Re: Specified cast is not valid

    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

    --
    RW


    "dlm@xxxxxx" wrote:

    > 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@xxxxxx
    > Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
    > www.bypsoft.com
    >
    >
    >
    > "RW" <RW@xxxxxx> schrieb im Newsbeitrag news:5CBC9DF7-92CE-4E6F-B78E-FD12A193BD61@xxxxxx

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

      My System SpecsSystem Spec

Specified cast is not valid problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of cast between c++ and c# ? Scotty .NET General 3 09 Apr 2010
"Specified cast is not valid." error Curious .NET General 0 14 Oct 2009
Type cast Joachim Meyer PowerShell 1 26 Aug 2008
meaning of this cast ? Leo Tohill PowerShell 3 15 Nov 2007
Verizon V-cast software justbob30 Vista General 2 14 Aug 2007