Function to return a Strongly Typed Object

Posted on 2007-07-30
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
    LVL 96

    Expert Comment

    by:Bob Learned
    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

    by:DigitalDan3 2005

    LVL 96

    Expert Comment

    by:Bob Learned

    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?


    Author Comment

    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

    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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    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…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now