470,810 Members | 1,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Could a delegate point to a constructor?

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
Feb 9 '06 #1
2 1084
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" <Th************@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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

Feb 9 '06 #2
>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.
Feb 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by IMS.Rushikesh | last post: by
5 posts views Thread by Edward Diener | last post: by
9 posts views Thread by Terry Olsen | last post: by
2 posts views Thread by zamir.khan | last post: by
1 post views Thread by Quimbly | last post: by
6 posts views Thread by RobKinney1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.