Could a delegate point to a constructor?

Discussion in 'Microsoft Dot NET' started by Guest, Feb 9, 2006.

  1. Guest

    Guest Guest

    Hi.
    I'm looking for a way to make a delegate point to a constructor.
    I'm not too sure i'm explaining it right,so here's a sample code to
    demonstrate.

    -----
    using System.Windows.Forms;
    class xyz
    {
    public delegate Form MyDelegate (string input);
    public static void func (MyDelegate x)
    {
    Form y = x("p");
    }
    }
    class abc : Form
    {
    public abc (string title) {...}
    ....
    }
    -----

    Now i would want to make the xyz.func() create a new "abc".
    both the "MyDelegate" and the "abc" constructor have the same return value
    and the same parameters,so theoretically there should be a way.

    Hopefully i'm clear enough for someone...
    Thank you
     
    Guest, Feb 9, 2006
    #1
    1. Advertisements

  2. No. But, you could create a factory method or class that can create an
    instance of the Form for you using something like
    Activator.CreateInstance(...). Then have the delegate point to the factory.

    "ThePinkPanther" <> wrote in message
    news:...
    > Hi.
    > I'm looking for a way to make a delegate point to a constructor.
    > I'm not too sure i'm explaining it right,so here's a sample code to
    > demonstrate.
    >
    > -----
    > using System.Windows.Forms;
    > class xyz
    > {
    > public delegate Form MyDelegate (string input);
    > public static void func (MyDelegate x)
    > {
    > Form y = x("p");
    > }
    > }
    > class abc : Form
    > {
    > public abc (string title) {...}
    > ...
    > }
    > -----
    >
    > Now i would want to make the xyz.func() create a new "abc".
    > both the "MyDelegate" and the "abc" constructor have the same return value
    > and the same parameters,so theoretically there should be a way.
    >
    > Hopefully i'm clear enough for someone...
    > Thank you
     
    Peter Rilling, Feb 9, 2006
    #2
    1. Advertisements

  3. >Now i would want to make the xyz.func() create a new "abc".

    In addition to what the others said, it's worth pointing out that a
    constructor doesn't have a return value so

    >both the "MyDelegate" and the "abc" constructor have the same return value
    >and the same parameters,so theoretically there should be a way.


    is incorrect. A constructor doesn't return a new object, it
    initializes a newly allocated one.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.
     
    Mattias Sjögren, Feb 9, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener

    Delegate constructor types

    Edward Diener, Jan 3, 2004, in forum: Microsoft Dot NET
    Replies:
    2
    Views:
    243
    Edward Diener
    Jan 3, 2004
  2. Edward Diener

    Multicast delegate checking for particular delegate

    Edward Diener, Oct 5, 2004, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    319
    Jon Skeet [C# MVP]
    Oct 5, 2004
  3. Guest
    Replies:
    0
    Views:
    652
    Guest
    Oct 20, 2004
  4. Guest
    Replies:
    0
    Views:
    540
    Guest
    Oct 20, 2004
  5. Curious

    Single-thread delegate vs multi-thread delegate

    Curious, May 5, 2010, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    1,277
    Family Tree Mike
    May 5, 2010
Loading...

Share This Page