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?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Grandma Wilkerson

    Calling methods from constructor?

    Grandma Wilkerson, Jul 17, 2003, in forum: Microsoft Dot NET
    Replies:
    2
    Views:
    662
    Jon Skeet
    Jul 17, 2003
  2. Henry Rollins

    Access Application variables in a page constructor

    Henry Rollins, Sep 4, 2003, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    241
    Dmitriy Lapshin [C# / .NET MVP]
    Sep 5, 2003
  3. Edward Diener

    Delegate constructor types

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

    Multicast delegate checking for particular delegate

    Edward Diener, Oct 5, 2004, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    498
    Jon Skeet [C# MVP]
    Oct 5, 2004
  5. Guest
    Replies:
    0
    Views:
    965
    Guest
    Oct 20, 2004
  6. Guest
    Replies:
    0
    Views:
    841
    Guest
    Oct 20, 2004
  7. Akula

    Point-to-Point protocol in .NET??

    Akula, Nov 16, 2005, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    352
    Akula
    Nov 21, 2005
  8. Curious

    Single-thread delegate vs multi-thread delegate

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