Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. get type of uninitialized object

get type of uninitialized object

Scheduled Pinned Locked Moved Visual Basic
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason188
    wrote on last edited by
    #1

    :(( I have a class in which I declare and expose all the forms in my application, I wish to have all form load operations/calls to be done by 1 subroutine in my class, thus...

    Public Class X

    Public Shared frm1 As myForm
    Public Shared frm2 As hisForm
    Public Shared frm3 As herForm
    Public Shared frm4 As ourForm
    
    Public Shared Sub LoadForm(ByRef theForm As Form)
    
        Dim t As Type
    
        t = theForm.GetType
        theForm = CType(Activator.CreateInstance(t), Form)
        theForm.Show()
    
    End Sub
    

    End Class

    The above code will not run because the object "theForm" has not been initialized, and obviously I cna't intialize it w/o knowing which form I am loading, it throws a "System.NullReferenceException" event. Any idea on how I could detect or get the object type, or type name of an object before it has been created/initialized? The code blows up at the line "t = theForm.GetType", any help is greatly appreciated, thank you.

    D 1 Reply Last reply
    0
    • J Jason188

      :(( I have a class in which I declare and expose all the forms in my application, I wish to have all form load operations/calls to be done by 1 subroutine in my class, thus...

      Public Class X

      Public Shared frm1 As myForm
      Public Shared frm2 As hisForm
      Public Shared frm3 As herForm
      Public Shared frm4 As ourForm
      
      Public Shared Sub LoadForm(ByRef theForm As Form)
      
          Dim t As Type
      
          t = theForm.GetType
          theForm = CType(Activator.CreateInstance(t), Form)
          theForm.Show()
      
      End Sub
      

      End Class

      The above code will not run because the object "theForm" has not been initialized, and obviously I cna't intialize it w/o knowing which form I am loading, it throws a "System.NullReferenceException" event. Any idea on how I could detect or get the object type, or type name of an object before it has been created/initialized? The code blows up at the line "t = theForm.GetType", any help is greatly appreciated, thank you.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      This was just answered earlier in the week. You cannot get the type of an uninstantiated class. Besides, the type in your method will always be Form, not the type you passed in.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        This was just answered earlier in the week. You cannot get the type of an uninstantiated class. Besides, the type in your method will always be Form, not the type you passed in.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        J Offline
        J Offline
        Jason188
        wrote on last edited by
        #3

        I get it, I'm SOL... :sigh:, thanx

        D 1 Reply Last reply
        0
        • J Jason188

          I get it, I'm SOL... :sigh:, thanx

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          No, you have to take a different approach to a Factory class. It's not good practice to make a a single factory for all different kinds of objects. What you've done in your code only complicates something as simple as:

          Dim newForm As New MyForm2
          

          You haven't said anything about why you want to do this, you just said this is the way you want to do it.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          J 1 Reply Last reply
          0
          • D Dave Kreskowiak

            No, you have to take a different approach to a Factory class. It's not good practice to make a a single factory for all different kinds of objects. What you've done in your code only complicates something as simple as:

            Dim newForm As New MyForm2
            

            You haven't said anything about why you want to do this, you just said this is the way you want to do it.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            J Offline
            J Offline
            Jason188
            wrote on last edited by
            #5

            well, I have one main search form which searches different type of data, say, user info, and company info for example, and in my search form I use one drop down to display the results based on what you searched for, so if you searched for users, then the combo box will show you a list of matching users, if you searched for companies, then you'll get a list of companies in the combo box. I wanted to make it so that when you click on the combo box, the app will call 1 method to load the corresponding form which will display and let you view/edit data. When you do the search I pre-set a val in my app that tells me what type of data we're looking at.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups