J
James Crosswell
I'm not sure if I'm going about this the right way - it may be that
Generics might be able to help me out here... but here goes:
I have three classes as follows
class BaseEdit
class WidgetEdit: BaseEdit
class FooEdit: BaseEdit
These are all acutally Windows Forms and BaseEdit is an abstract class
(and so never gets instantiated). I'd like to define a method in
BaseEdit called EditSelected, which takes a proprietary grid control as
a parameter, checks to see if any items/rows in the grid are selected
and, if so, opens the creates and instance of whichever derived class
was used to call the method and shows this... I've seen various examples
of polymorphism but none that actually instantiate derived classes.
So basically what I'd like is:
class BaseEdit {
//...
public static void EditSelected(GridControl aGrid)
{
if (aGrid.SelectedRecordCount > 0)
{
BaseEdit editForm = [create instance of derived class here];
editForm.EditItem(aGrid.SelectedRow["Id"]);
}
}
//...
}
And in the code for the application I'd write something like
WidgetEdit.EditSelected( someGrid );
Is this possible? I know I could do this by overriding constructors but
I don't actually want to have to create an instance of the derived class
unless it is necessary.
Alternatively, and maybe this is what Generics are for although most of
the docs I've stumbled upon seem to refer to them in the context of
collections, I could maybe make a method that took a "Class Parameter" -
as a Delphi programmer I'm not sure what you'd call that in c#...
basically I need something which is to classes what Delegates are to
methods... so I can pass into a method:
public static void EditSelected<SomeClass>
{
SomeClass editForm = new SomeClass();
editForm.EditItem(aGrid.SelectedRow["Id"]);
}
But if it was a Generic then there'd be no way of knowing if it had an
EditItem method... I don't think... so maybe I need to throw some
interfaces in there as well.
The more I think about this the more I get the feeling I'm going about
this the wrong way in any case. Can anyone help?
Thanks in advance.
Best Regards,
James Crosswell
Microforge.net LLC
http://www.microforge.net
Generics might be able to help me out here... but here goes:
I have three classes as follows
class BaseEdit
class WidgetEdit: BaseEdit
class FooEdit: BaseEdit
These are all acutally Windows Forms and BaseEdit is an abstract class
(and so never gets instantiated). I'd like to define a method in
BaseEdit called EditSelected, which takes a proprietary grid control as
a parameter, checks to see if any items/rows in the grid are selected
and, if so, opens the creates and instance of whichever derived class
was used to call the method and shows this... I've seen various examples
of polymorphism but none that actually instantiate derived classes.
So basically what I'd like is:
class BaseEdit {
//...
public static void EditSelected(GridControl aGrid)
{
if (aGrid.SelectedRecordCount > 0)
{
BaseEdit editForm = [create instance of derived class here];
editForm.EditItem(aGrid.SelectedRow["Id"]);
}
}
//...
}
And in the code for the application I'd write something like
WidgetEdit.EditSelected( someGrid );
Is this possible? I know I could do this by overriding constructors but
I don't actually want to have to create an instance of the derived class
unless it is necessary.
Alternatively, and maybe this is what Generics are for although most of
the docs I've stumbled upon seem to refer to them in the context of
collections, I could maybe make a method that took a "Class Parameter" -
as a Delphi programmer I'm not sure what you'd call that in c#...
basically I need something which is to classes what Delegates are to
methods... so I can pass into a method:
public static void EditSelected<SomeClass>
{
SomeClass editForm = new SomeClass();
editForm.EditItem(aGrid.SelectedRow["Id"]);
}
But if it was a Generic then there'd be no way of knowing if it had an
EditItem method... I don't think... so maybe I need to throw some
interfaces in there as well.
The more I think about this the more I get the feeling I'm going about
this the wrong way in any case. Can anyone help?
Thanks in advance.
Best Regards,
James Crosswell
Microforge.net LLC
http://www.microforge.net