Results 1 to 9 of 9

Thread: need help with CreateObject equivalent in C#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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?

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    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);

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with CreateObject equivalent in C#

    thanks

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with CreateObject equivalent in C#

    By the way, do you know how to SET and GET public properties of the "MyObject"?

  5. #5
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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#?

  7. #7
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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()).

  8. #8
    Fanatic Member kaihirst's Avatar
    Join Date
    Jul 2005
    Location
    The Resaurant At the End of The Universe
    Posts
    633

    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
    As the information I give is useful in its nature, consider using the RATE POST feature located on the bottom left of this post please..

    A few things that make a good Developer a Great One.
    Methodical and a thorough approach to research and design inevitably leads to success.
    Forward thinking is the key to Flow of control.
    Never test in the design environment, always test in real time, you get the REAL results.
    CBSE & OOSE are the same animal, they just require different techniques, and thinking.
    SEO is a globe of objectives, SE rankings is an end to a means for these objectives, not part of them.
    The key to good design is explicit attention to both detail and response.
    Think Freely out of the "Box" you're in..... You will soar to better heights.

    Kai Hirst - MSCE, MCDBA, MCSD, MCP, MCAP, MSCT


  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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"

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