Could a delegate point to a constructor?


G

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
 
Ad

Advertisements

P

Peter Rilling

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.
 
Ad

Advertisements

M

Mattias Sjögren

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
 

Ask a Question

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. After that, you can post your question and our members will help you out.

Ask a Question

Top