CreateObject in VB.NET

Hi all
is there any function in VB.NET does the same job as CreateObject in VB6?
VB6 Code:
Dim objDefine As Object
Set objDefine = CreateObject("prjSysDefine.clsStart")
Thanks
[222 byte] By [yolip] at [2007-11-20 0:41:58]
# 1 Re: CreateObject in VB.NET
Hello yollip! :wave:

CreateObject is still around in VB.NET, here's a couple of links demonstrating its use:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctCreateObject.asp

http://www.samspublishing.com/articles/article.asp?p=25857

http://msdn.microsoft.com/library/en-us/dno2k3ta/html/odc_VBNETcallsVBA.asp?frame=true

I hope it helps!
HanneSThEGreaT at 2007-11-10 3:12:48 >
# 2 Re: CreateObject in VB.NET
VB.NET has the New statement for all object creation.
Dim obj As myClass
obj = New myClass()in short
Dim obj As New myClass()

CreateObject is used only to create classic COM components and won't create an instance of a .NET component. The replacement for CreateObject is the System.Activator. CreateInstance method.
aniskhan at 2007-11-10 3:13:52 >
# 3 Re: CreateObject in VB.NET
with the new statement i've tried, it has to add reference before using it... unlike in vb6, it doesn't like late binding
yolip at 2007-11-10 3:14:52 >
# 4 Re: CreateObject in VB.NET
Thanks guys...but i think i need to provide more information to get correct answer. What i am trying to do is i will create multiple projects.
eg. one main project and multiple library class projects (will become dll after compiled). I wish to call the library class project from main or other projects without pre-reference using add reference function. It is great if can work as in VB6. I need more flexibilities.
yolip at 2007-11-10 3:15:46 >
# 5 Re: CreateObject in VB.NET
Check out The System.Reflection namespace, and articles and discussions over Reflection in VB.NET. I believe that is what you want. It is not a particularly easy subject to go over, but it can do what you are wanting to do...

Some of my bookmarks over it:
http://www.programmersheaven.com/2/Dot-Net-Reflection-Part-1-Page2
http://www.ondotnet.com/pub/a/dotnet/2003/11/17/reflectionpt2.html
gigemboy at 2007-11-10 3:16:45 >
# 6 Re: CreateObject in VB.NET
Thanks...but i can't really sort that out... could you guys give me an example?
yolip at 2007-11-10 3:17:53 >
# 7 Re: CreateObject in VB.NET
Look at Activator.CreateInstance . There are different overloads depending on what you are needing, but one of them takes a Type as a parameter. Below is an example of it from MSDN Help..

' Create an instance of the StringBuilder type using Activator.CreateInstance.
Dim o As Object = Activator.CreateInstance(GetType(StringBuilder))

Now remember that the return value is of type object, so if you wish to use the properties and methods of that object, you have to cast it to the type that it is...

But if this is a COM object, you still have the CreateObject function, if you care to use it.

Creates and returns a reference to a COM object. CreateObject cannot be used to create instances of classes in Visual Basic unless those classes are explicitly exposed as COM components.

Public Shared Function CreateObject( _
ByVal ProgId As String, _
Optional ByVal ServerName As String = "" _
) As Object

Parameters
ProgId
Required. String. The program ID of the object to create.

ServerName
Optional. String. The name of the network server where the object will be created. If ServerName is an empty string (""), the local computer is used.
...
Another issue is that COM objects use unmanaged code code without the benefit of the common language runtime. There is a fair degree of complexity involved in mixing the managed code of Visual Basic with unmanaged code from COM. When you add a reference to a COM object, Visual Basic searches for a primary interop assembly (PIA) for that library; if it finds one, then it uses it. If it does not find a PIA, then it creates an interoperability assembly that contains local interoperability classes for each class in the COM library.
gigemboy at 2007-11-10 3:18:50 >
# 8 Re: CreateObject in VB.NET
Thanks guys, but i get lost again... is that hard to have clear solution?
yolip at 2007-11-10 3:19:53 >
# 9 Re: CreateObject in VB.NET
Have you tried Activator.CreateInstance?

Dim o As Object = Activator.CreateInstance(GetType(prjSysDefine.clsStart))

We do not know what type of object you are using, since you haven't specified what type it is, whether it is managed, unmanaged, COM, Interop, etc., so how can we give you a definite answer? It wouldn't hurt to try some of the suggestions already mentioned above, however...

And, as already mentioned, you still have CreateObject... type it in... its there...
gigemboy at 2007-11-10 3:20:52 >
# 10 Re: CreateObject in VB.NET
anyone has a simple solution? a clear example?
yolip at 2007-11-10 3:21:52 >