need help with CreateObject equivalent in C#
I've tried CreateObject and it does not work in C#. So i tried:
Type t = Type.GetTypeFromProgID("MyObject.Class1");
Object o = Activator.CreateInstance(t);
This works to the point where it compiles and runs. However, when I try to use one of MyObject's methods, it gives me an error:
'object' does not contain a definition for 'MyFunction'
Any ideas on how to do this correctly?
Re: need help with CreateObject equivalent in C#
This is off the top of my head and I'm not at a machine where I can test it but:
Create an Interface which your class implements
then instead of using an object use the interface
Type t = Type.GetTypeFromProgID("MyObject.Class1");
interface1 o = (interface1)Activator.CreateInstance(t);
Re: need help with CreateObject equivalent in C#
Re: need help with CreateObject equivalent in C#
By the way, do you know how to SET and GET public properties of the "MyObject"?
Re: need help with CreateObject equivalent in C#
Quote:
Originally Posted by benmartin101
By the way, do you know how to SET and GET public properties of the "MyObject"?
I'm not sure what you mean? If you use an interface you'll know what methods are available. To enumerate all the methods you can use the MethodInfo,MemberInfo and ParamaterInfo classes in conjunction with the Type class which has a number of get methods GetMethod, get Memeber etc.
hmm, how many times can I fit the word "method" into a paragraph.
Re: need help with CreateObject equivalent in C#
No, i mean like...my class has a name property. In vb, the code would be:
dim obj as new MyObject
obj.name = "john"
how do I do that in c#?
Re: need help with CreateObject equivalent in C#
Do it just like the vb (obj.property) , with the dot property. If it's not working, then that property is declared as private, either declare the property as public or create a getmethod for the private property so (obj.getProperty()).
Re: need help with CreateObject equivalent in C#
Hi,
best way in c# to do this is to overload it, then assign the contents upon the object instantiation
class clsClass
{
private string me;
private stirng you;
public clsClass(string me, string you)
{
this.me = me;
this.you = you;
}
public virtual void DecalreValues()
{
// define any mothodology here such as declaring obvjects blah blah
}
}
public static void Main()
{
clsClass youandme = new clsClass("kaihirst", "benmartin101")
or you could do it like ...
youandme.me = "kaihirst";
youandme.you = "benmartin101";
//decalring a method is still the same throughout, in case you dont know
youandme.DeclareValues()
}
hope this helps.
ta
kai :wave:
Re: need help with CreateObject equivalent in C#
I can't make changes to it. It's a COM dll. I just wanted to know how I would access their properties and functions if I was to do "late binding" instead of "early binding"