Results 1 to 4 of 4

Thread: [RESOLVED] [2005] .NET equivalent of COM's CreateObject

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Resolved [RESOLVED] [2005] .NET equivalent of COM's CreateObject

    How do you create an object instance if all you have is the name of the class?
    I want to add controls (derived from webcontrols) onto a webpage at runtime. Information is stored in a database, so all I know is the class name of the control, ie

    Code:
        Public Class wssList
            Inherits System.Web.UI.WebControls.BulletedList
            Public Sub New()
                Me.Items.Add("Test 1")
                Me.Items.Add("Test 2")
            End Sub
        End Class
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ctl As System.Web.UI.Control
            Dim str as String = "wssList"
            ctl = New ?????????
            Me.Forms.Controls.Add(ctl)
        End Sub
    Last edited by brucevde; Sep 12th, 2007 at 03:51 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] .NET equivalent of COM's CreateObject

    Take a look at Activator.CreateInstance method.... Frankly speaking, I've never had the need to use it, so I can't guide you any further.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] .NET equivalent of COM's CreateObject

    I've only ever used .CreateInstance with Office. I would think if you have a COM web controls you would instanciate them using the New keyword as long as you have a reference set to the dll.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [2005] .NET equivalent of COM's CreateObject

    I found a couple ways of creating the instance and there are probably more. I am not sure which is the better code to use or if it even makes a difference. I will probably use the ObjectHandle method as it would allow me to package the controls in a different Assembly...

    Thanks for the help. Here is the code if anyone else has the same question...

    Code:
    Dim handle As System.Runtime.Remoting.ObjectHandle
    Dim ctl As System.Web.UI.WebControls.WebControl
    
    handle  = System.Activator.CreateInstance(Nothing, "wssList")
    ctl = handle.Unwrap
    Controls.Add(ctl)
    
    Dim oType As System.Type
    Dim ctl As System.Web.UI.WebControls.WebControl
    
    oType = Type.GetType("wssList", True, True)
    ctl = System.Activator.CreateInstance(oType)
    Controls.Add(ctl)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width