Link to home
Start Free TrialLog in
Avatar of DigitalDan3
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(ByVal object as Object) as object.gettype(object)

That way I could create a new object of type myObject.getype and assign it to InstanciateObjectType(myObject)

Dim myObjectType as Type = myObject.GetType()
Dim MyInstanciatedObject as myObjectType = InstanciateObjectType(myObject)

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.OfficeName
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What version of VB.NET are you using?  Do you have 2005?  If you do, then you should be able to use generics.

Bob
Avatar of DigitalDan3
DigitalDan3

ASKER

VB.net 2005

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


ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(Type.GetType("WindowsApplication1.OfficeManager"))

My original version of UserInfo contained a property for each type of RoleProfile I created.  

If User.IsInRole("Office Manager") then
   If Not UserInfo.OfficeMangerProfile is Nothing Then
      Label1.Text = UserInfo.OfficeMangerProfile.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(ByVal 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