CreateObject in C#

  • Thread starter Thread starter pa
  • Start date Start date
P

pa

What's the C# equivalent to the VB 'CreateObject' function.
I know there's a Server.CreateObject but I need to do this
from a windows app that doesn't use the web.

Here's the VB code I want to convert.

Dim obj as Object
obj = CreateObject("Some.Application")

Thanks for your help
pa
 
Here's the VB code I want to convert.

Dim obj as Object
obj = CreateObject("Some.Application")

object obj =
Activator.CreateInstance(Type.GetTypeFromProgID("Some.Application"));



Mattias
 
Mattias,
Thanks for the quick response. However, when i run the
code i get an error trying to call one of the objects
methods:

//this compiles fine
object objPB = Activator.CreateInstance
(Type.GetTypeFromProgID("Powerbuilder.Application"));

//below produces the following errors:
//'object' does not contain a definition for 'LibraryList'
objPB.LibraryList = "c:\\web\\medsrg08.pbd";
objPB.MachineCode = False;

thanks
pa
 
That's because you have only an object reference, not a strongly
typed reference to the COM object.

This is basic stuff, I suggest you take some online tutorials
as basics for object-oriented programming and strong-typed
languages such as .NET/C#. VB is not a strong-typed language
(well, not very strong typed, it has a little), so this is
probably new to you.

The way Mattias mentioned is the quick and dirty way.
You don't have a strong-typed reference, so if you wish
to call the LibraryList property on the object, you'll
have to use reflection to get an handle to the
PropertyInfo for that object, etc.

The preferred way is to import the typelib for the COM
object and let .NET create a .NET wrapper around the COM
object for you.

If you're using VS.NET, just simple add a reference to
the COM object by Project->Add Reference and select the
COM tab and find your COM library.

If you're not using VS.NET, use the tlbimp.exe utility
and give it the DLL that hosts the COM object you want
like:

tlbimp PowerBuilder.dll.

-c


Mattias,
Thanks for the quick response. However, when i run the
code i get an error trying to call one of the objects
methods:

//this compiles fine
object objPB = Activator.CreateInstance
(Type.GetTypeFromProgID("Powerbuilder.Application"));

//below produces the following errors:
//'object' does not contain a definition for 'LibraryList'
objPB.LibraryList = "c:\\web\\medsrg08.pbd";
objPB.MachineCode = False;

thanks
pa
 
Back
Top