DigitalDan3
asked on
Function to return a Strongly Typed Object
I would like to create a function that accepts and object and returns an instance of that object.
Function InstanciateObjectType(ByVa l object as Object) as object.gettype(object)
That way I could create a new object of type myObject.getype and assign it to InstanciateObjectType(myOb ject)
Dim myObjectType as Type = myObject.GetType()
Dim MyInstanciatedObject as myObjectType = InstanciateObjectType(myOb ject)
Say I have a class of OfficeManager which stores the OfficeName as String
And I have a class of RegionalManager which stores the RegionID as Integer()
if I pass in an OfficeManager object I should see in intellisense
MyInstanciatedObject.Offic eName
Function InstanciateObjectType(ByVa
That way I could create a new object of type myObject.getype and assign it to InstanciateObjectType(myOb
Dim myObjectType as Type = myObject.GetType()
Dim MyInstanciatedObject as myObjectType = InstanciateObjectType(myOb
Say I have a class of OfficeManager which stores the OfficeName as String
And I have a class of RegionalManager which stores the RegionID as Integer()
if I pass in an OfficeManager object I should see in intellisense
MyInstanciatedObject.Offic
ASKER
VB.net 2005
Dan
Dan
Dan,
With generics, you could do it this way:
Public Class ObjectCreator(Of T As {New})
Public Shared Function GetInstance()
Return New T
End Function
End Class
But, the question really is, "Why do it that way?". You could just do this:
Dim mgr As New OfficeManager()
What are you really trying to accomplish here?
Bob
With generics, you could do it this way:
Public Class ObjectCreator(Of T As {New})
Public Shared Function GetInstance()
Return New T
End Function
End Class
But, the question really is, "Why do it that way?". You could just do this:
Dim mgr As New OfficeManager()
What are you really trying to accomplish here?
Bob
ASKER
I am using profiles for both ASP and Winforms. I have created a class called UserInfo which stores profile information that is common to all Users.. ie. FirstName, LastName, PhoneNumber etc it also has a property called RoleProfile which is of type Object. This property stores profile information that is specific to the Role the user is assigned to. So if I user is an OfficeManager then I store an OfficeManger object in the RoleProfile property and so on for the different Role Types.
I am trying to find a way that would create a function that would return an instance of the type contained in tin the RoleProfile property. Basically pass in a general object and return an strongly typed instance of that object type.
I am trying to find a way that would create a function that would return an instance of the type contained in tin the RoleProfile property. Basically pass in a general object and return an strongly typed instance of that object type.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
obj is still an System.Object and not an OfficeManger correct?
obj.OfficeID would be illegal correct.
I would have to either
Dim objOfficeManger as OfficeManager = CTYPE(obj, OfficeManager)
Or
Dim objOfficeManager as OfficeManager = Activator.CreateInstance(T ype.GetTyp e("Windows Applicatio n1.OfficeM anager"))
My original version of UserInfo contained a property for each type of RoleProfile I created.
If User.IsInRole("Office Manager") then
If Not UserInfo.OfficeMangerProfi le is Nothing Then
Label1.Text = UserInfo.OfficeMangerProfi le.Office
End If
End If
I thought that better OOP practices would be to create a single property of type object to store this information.
I could create a Function for each type that is stored in RoleProfile.
Public Function GetOfficeMangerProfile(ByV al object as Object) as OfficeManger
Dim OfficeManger as OfficeManager = TryCast(object, OfficeManger)
Return OfficeManager
End Function.
I would have to do this for each type that is stored in RoleProfile. Currently I have 12 different RoleProfile Types. I wanted to do this in a single Function that would accept and object and return an instance of the type of object passed in.
Thanks Dan
obj.OfficeID would be illegal correct.
I would have to either
Dim objOfficeManger as OfficeManager = CTYPE(obj, OfficeManager)
Or
Dim objOfficeManager as OfficeManager = Activator.CreateInstance(T
My original version of UserInfo contained a property for each type of RoleProfile I created.
If User.IsInRole("Office Manager") then
If Not UserInfo.OfficeMangerProfi
Label1.Text = UserInfo.OfficeMangerProfi
End If
End If
I thought that better OOP practices would be to create a single property of type object to store this information.
I could create a Function for each type that is stored in RoleProfile.
Public Function GetOfficeMangerProfile(ByV
Dim OfficeManger as OfficeManager = TryCast(object, OfficeManger)
Return OfficeManager
End Function.
I would have to do this for each type that is stored in RoleProfile. Currently I have 12 different RoleProfile Types. I wanted to do this in a single Function that would accept and object and return an instance of the type of object passed in.
Thanks Dan
Bob