CreateObject function 
Author Message
 CreateObject function

Hello everyone!

I've a problem with the function "CreateObject". It is present and work fine
when I write a VB.Net class, but the compiler report an error (function not
found) when a try to use it in a C# project. the references list contain the
same objects, and I imports the same assemblies. I can't understand why it
doesn't work.

Thanks in advance.

Lorenzo



Mon, 23 May 2005 00:47:13 GMT  
 CreateObject function

Quote:
> I've a problem with the function "CreateObject". It is present and work
fine
> when I write a VB.Net class, but the compiler report an error (function
not
> found) when a try to use it in a C# project. the references list contain
the
> same objects, and I imports the same assemblies. I can't understand why it
> doesn't work.

Hi, objects in C# are created using the "new" operator.

-- Alan



Mon, 23 May 2005 00:54:29 GMT  
 CreateObject function
Lorenzo,

    If you want to call CreateObject the same way that you call it in VB,
then set a reference to Microsoft.VisualBasic.dll, and use the static
CreateObject method on the Interaction class in the Microsoft.VisualBasic
namespace.

    However, if you want to use the way that .NET uses, you might want to
consider using one of the static methods on the Activator class,
specifically CreateInstance or CreateInstanceFrom.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Hello everyone!

> I've a problem with the function "CreateObject". It is present and work
fine
> when I write a VB.Net class, but the compiler report an error (function
not
> found) when a try to use it in a C# project. the references list contain
the
> same objects, and I imports the same assemblies. I can't understand why it
> doesn't work.

> Thanks in advance.

> Lorenzo



Mon, 23 May 2005 00:51:55 GMT  
 CreateObject function
Thanks for your help!
 I'm going to have a look at these methods.
By the way, my problem is to create an instance of a COM Object without
reference it in the IDE. I need it because I don't know wich version is
installed and I don't want to know it because I need functions common to all
versions.

I'm going to work. Thanks.
Bye

Lorenzo



Quote:
> Lorenzo,

>     If you want to call CreateObject the same way that you call it in VB,
> then set a reference to Microsoft.VisualBasic.dll, and use the static
> CreateObject method on the Interaction class in the Microsoft.VisualBasic
> namespace.

>     However, if you want to use the way that .NET uses, you might want to
> consider using one of the static methods on the Activator class,
> specifically CreateInstance or CreateInstanceFrom.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET/C# MVP]



> > Hello everyone!

> > I've a problem with the function "CreateObject". It is present and work
> fine
> > when I write a VB.Net class, but the compiler report an error (function
> not
> > found) when a try to use it in a C# project. the references list contain
> the
> > same objects, and I imports the same assemblies. I can't understand why
it
> > doesn't work.

> > Thanks in advance.

> > Lorenzo



Mon, 23 May 2005 01:12:39 GMT  
 CreateObject function
This code works, though it's a bit fuzzy invoking methods on it since you're
working with an "object". In VB you can have an object o and invoke
o.DoStuff() - VB.NET will compile fine and throw a runtime error only if the
method doesn't exist, while C# will throw a compilation error since object
doesn't have any DoStuff method. Instead use the Invoke method below.

public static object CreateObject(string ProgID)
{
  Type objType = Type.GetTypeFromProgID(ProgID);
  return Activator.CreateInstance(objType);

Quote:
}

// haven't tested this code but it should work

public static object Invoke(object target, string name, params object[]
parameters)
{
  if (target== null)
    return null;

  return target.GetType().InvokeMember(
    name,
    BindingFlags.InvokeMethod,
    null,
    target,
    parameters);

Quote:
}

Good luck !

Best regards,

Dag Christensen


Quote:
> Thanks for your help!
>  I'm going to have a look at these methods.
> By the way, my problem is to create an instance of a COM Object without
> reference it in the IDE. I need it because I don't know wich version is
> installed and I don't want to know it because I need functions common to
all
> versions.

> I'm going to work. Thanks.
> Bye

> Lorenzo



> > Lorenzo,

> >     If you want to call CreateObject the same way that you call it in
VB,
> > then set a reference to Microsoft.VisualBasic.dll, and use the static
> > CreateObject method on the Interaction class in the

Microsoft.VisualBasic

- Show quoted text -

Quote:
> > namespace.

> >     However, if you want to use the way that .NET uses, you might want
to
> > consider using one of the static methods on the Activator class,
> > specifically CreateInstance or CreateInstanceFrom.

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET/C# MVP]



> > > Hello everyone!

> > > I've a problem with the function "CreateObject". It is present and
work
> > fine
> > > when I write a VB.Net class, but the compiler report an error
(function
> > not
> > > found) when a try to use it in a C# project. the references list
contain
> > the
> > > same objects, and I imports the same assemblies. I can't understand
why
> it
> > > doesn't work.

> > > Thanks in advance.

> > > Lorenzo



Mon, 23 May 2005 15:39:45 GMT  
 CreateObject function
Thanks again for your help. I'll try your code today.
Bye
Lorenzo

"D Christensen" <dag_at_clara_dot_co_dot_uk> ha scritto nel messaggio

Quote:
> This code works, though it's a bit fuzzy invoking methods on it since
you're
> working with an "object". In VB you can have an object o and invoke
> o.DoStuff() - VB.NET will compile fine and throw a runtime error only if
the
> method doesn't exist, while C# will throw a compilation error since object
> doesn't have any DoStuff method. Instead use the Invoke method below.

> public static object CreateObject(string ProgID)
> {
>   Type objType = Type.GetTypeFromProgID(ProgID);
>   return Activator.CreateInstance(objType);
> }

> // haven't tested this code but it should work

> public static object Invoke(object target, string name, params object[]
> parameters)
> {
>   if (target== null)
>     return null;

>   return target.GetType().InvokeMember(
>     name,
>     BindingFlags.InvokeMethod,
>     null,
>     target,
>     parameters);
> }

> Good luck !

> Best regards,

> Dag Christensen



> > Thanks for your help!
> >  I'm going to have a look at these methods.
> > By the way, my problem is to create an instance of a COM Object without
> > reference it in the IDE. I need it because I don't know wich version is
> > installed and I don't want to know it because I need functions common to
> all
> > versions.

> > I'm going to work. Thanks.
> > Bye

> > Lorenzo


ha

> > > Lorenzo,

> > >     If you want to call CreateObject the same way that you call it in
> VB,
> > > then set a reference to Microsoft.VisualBasic.dll, and use the static
> > > CreateObject method on the Interaction class in the
> Microsoft.VisualBasic
> > > namespace.

> > >     However, if you want to use the way that .NET uses, you might want
> to
> > > consider using one of the static methods on the Activator class,
> > > specifically CreateInstance or CreateInstanceFrom.

> > >     Hope this helps.

> > > --
> > >                - Nicholas Paldino [.NET/C# MVP]



> > > > Hello everyone!

> > > > I've a problem with the function "CreateObject". It is present and
> work
> > > fine
> > > > when I write a VB.Net class, but the compiler report an error
> (function
> > > not
> > > > found) when a try to use it in a C# project. the references list
> contain
> > > the
> > > > same objects, and I imports the same assemblies. I can't understand
> why
> > it
> > > > doesn't work.

> > > > Thanks in advance.

> > > > Lorenzo



Mon, 23 May 2005 17:09:30 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. CreateObject method in c# ?

2. CreateObject

3. Found the "createObject". Got casting problem

4. looking for the VB CreateObject equivalent

5. How to convert VB CreateObject() into C#.

6. Help with Server.CreateObject("Msxml2.ServerXMLHTTP")

7. CreateObject

8. Createobject in C

9. VB activation: CreateObject Vs 'New' operator

10. How to createobject like vb in vc++ without the ref of typelib

11. SetWindowsHookEx & CreateObject

12. Why New works and CreateObject fails ???

 

 
Powered by phpBB® Forum Software