[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Function to return a Strongly Typed Object

Posted on 2007-07-30
Medium Priority
Last Modified: 2010-04-23
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
Question by:DigitalDan3
  • 3
  • 3
LVL 96

Expert Comment

by:Bob Learned
ID: 19594567
What version of VB.NET are you using?  Do you have 2005?  If you do, then you should be able to use generics.


Author Comment

ID: 19594679
VB.net 2005

LVL 96

Expert Comment

by:Bob Learned
ID: 19594784

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?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 19594965
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.

LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 19595041
Try something like this sample:

    Dim obj As Object = Activator.CreateInstance(Type.GetType("Namespace.Class"))


    Dim obj As Object = Activator.CreateInstance(Type.GetType("WindowsApplication1.OfficeManager"))


Author Comment

ID: 19595429
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)


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


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question