get type of uninitialized object
-
:(( 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.
-
:(( 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.
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
-
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
-
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
-
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
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.