Home All Groups Group Topic Archive Search About

How do you call a method common to several user controls?

Author
27 Mar 2005 10:27 PM
Byron
I have several user controls that have a few methods in common, such
LoadFromForm() which populates an object from controls on the form.  I want
to call that method from the form in which the control is contained
regardless of the type currently displayed without having to use a huge
switch somthing like:

switch(curentUserType.GetType())
{
case "Person":
((person)currentUserControl).LoadFromForm();
....
case "Animal"
((animal)currentUserControl).LoadFromForm();
....
}

I assume there is something like:

((currentUserControl.GetType())currentUserControl).LoadFromForm();

available to call the LoadFromForm method regardless of the user control
type, but I can't figure out how to do it.

Thanks in advance for any help.

Author
27 Mar 2005 10:59 PM
Nick Malik [Microsoft]
Create an interface that defines the common methods, and make sure that all
of your user controls inherit from that method.
Then you can call your common method without casting.

No need for a switch stmt.  Let Object Oriented development do all the work.


--
--- Nick Malik [Microsoft]
    MCSD, CFPS, Certified Scrummaster
    http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
   I do not answer questions on behalf of my employer.  I'm just a
programmer helping programmers.
--
Show quote
"Byron" <By***@discussions.microsoft.com> wrote in message
news:8DF2E57E-560E-427B-B60E-5C07EF323111@microsoft.com...
>I have several user controls that have a few methods in common, such
> LoadFromForm() which populates an object from controls on the form.  I
> want
> to call that method from the form in which the control is contained
> regardless of the type currently displayed without having to use a huge
> switch somthing like:
>
> switch(curentUserType.GetType())
> {
> case "Person":
> ((person)currentUserControl).LoadFromForm();
> ...
> case "Animal"
> ((animal)currentUserControl).LoadFromForm();
> ...
> }
>
> I assume there is something like:
>
> ((currentUserControl.GetType())currentUserControl).LoadFromForm();
>
> available to call the LoadFromForm method regardless of the user control
> type, but I can't figure out how to do it.
>
> Thanks in advance for any help.
Author
28 Mar 2005 1:17 AM
Byron
I'm currently using an abstract class from which the controls I am interested
in inherit and the methods I'm interested in are public as well as abstract
in that class.  Could I be able to use the technique you suggest in this
case, or do I have to switch to interfaces?

Could you point me to a good source of examples and/or explanations of how
to pull this off?

I am using user controls that I add to a forms control collection to display
them.  These controls all have several methods in common with different
implementations and those methods are overridden from the same abstract
class.  Based on menu selections I want to execute the method in the user
controls within the forms control collection.

In the abstract class:
public abstract void Save(SqlConnection conn);

In the user control that inherits:
public override void Save()
{
....
}

On the main form that contains the control:
private void btnSave_Click(object sender, System.EventArgs e)
{
// How would I pull this off?
this.Controls[i].Save();

//OR as a form field
currentUserControl.Save();
}






Show quote
"Nick Malik [Microsoft]" wrote:

> Create an interface that defines the common methods, and make sure that all
> of your user controls inherit from that method.
> Then you can call your common method without casting.
>
> No need for a switch stmt.  Let Object Oriented development do all the work.
>
>
> --
> --- Nick Malik [Microsoft]
>     MCSD, CFPS, Certified Scrummaster
>     http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
>    I do not answer questions on behalf of my employer.  I'm just a
> programmer helping programmers.
> --
> "Byron" <By***@discussions.microsoft.com> wrote in message
> news:8DF2E57E-560E-427B-B60E-5C07EF323111@microsoft.com...
> >I have several user controls that have a few methods in common, such
> > LoadFromForm() which populates an object from controls on the form.  I
> > want
> > to call that method from the form in which the control is contained
> > regardless of the type currently displayed without having to use a huge
> > switch somthing like:
> >
> > switch(curentUserType.GetType())
> > {
> > case "Person":
> > ((person)currentUserControl).LoadFromForm();
> > ...
> > case "Animal"
> > ((animal)currentUserControl).LoadFromForm();
> > ...
> > }
> >
> > I assume there is something like:
> >
> > ((currentUserControl.GetType())currentUserControl).LoadFromForm();
> >
> > available to call the LoadFromForm method regardless of the user control
> > type, but I can't figure out how to do it.
> >
> > Thanks in advance for any help.
>
>
>
Author
28 Mar 2005 4:44 AM
Adam Clauss
Show quote
"Byron" <By***@discussions.microsoft.com> wrote in message
news:B3DD42BF-67F5-40B8-BB5D-35AF8E1800B6@microsoft.com...
> I'm currently using an abstract class from which the controls I am
> interested
> in inherit and the methods I'm interested in are public as well as
> abstract
> in that class.  Could I be able to use the technique you suggest in this
> case, or do I have to switch to interfaces?
>
> Could you point me to a good source of examples and/or explanations of how
> to pull this off?
>
> I am using user controls that I add to a forms control collection to
> display
> them.  These controls all have several methods in common with different
> implementations and those methods are overridden from the same abstract
> class.  Based on menu selections I want to execute the method in the user
> controls within the forms control collection.
>
> In the abstract class:
> public abstract void Save(SqlConnection conn);
>
> In the user control that inherits:
> public override void Save()
> {
> ...
> }
>
> On the main form that contains the control:
> private void btnSave_Click(object sender, System.EventArgs e)
> {
> // How would I pull this off?
> this.Controls[i].Save();

((YourAbstractClass)this.Controls[i]).Save();

except you need to make sure each control you are accessing (Controls[i] is
of that type).
You might want to do something like:

if (Controls[i] is YourAbstractClass)
    ((YourAbstractClass)this.Controls[i]).Save();


--
Adam Clauss
caba***@tamu.edu
Author
28 Mar 2005 1:17 AM
Byron
I'm currently using an abstract class from which the controls I am interested
in inherit and the methods I'm interested in are public as well as abstract
in that class.  Could I be able to use the technique you suggest in this
case, or do I have to switch to interfaces?

Could you point me to a good source of examples and/or explanations of how
to pull this off?

I am using user controls that I add to a forms control collection to display
them.  These controls all have several methods in common with different
implementations and those methods are overridden from the same abstract
class.  Based on menu selections I want to execute the method in the user
controls within the forms control collection.

In the abstract class:
public abstract void Save(SqlConnection conn);

In the user control that inherits:
public override void Save()
{
....
}

On the main form that contains the control:
private void btnSave_Click(object sender, System.EventArgs e)
{
// How would I pull this off?
this.Controls[i].Save();

//OR as a form field
currentUserControl.Save();
}






Show quote
"Nick Malik [Microsoft]" wrote:

> Create an interface that defines the common methods, and make sure that all
> of your user controls inherit from that method.
> Then you can call your common method without casting.
>
> No need for a switch stmt.  Let Object Oriented development do all the work.
>
>
> --
> --- Nick Malik [Microsoft]
>     MCSD, CFPS, Certified Scrummaster
>     http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
>    I do not answer questions on behalf of my employer.  I'm just a
> programmer helping programmers.
> --
> "Byron" <By***@discussions.microsoft.com> wrote in message
> news:8DF2E57E-560E-427B-B60E-5C07EF323111@microsoft.com...
> >I have several user controls that have a few methods in common, such
> > LoadFromForm() which populates an object from controls on the form.  I
> > want
> > to call that method from the form in which the control is contained
> > regardless of the type currently displayed without having to use a huge
> > switch somthing like:
> >
> > switch(curentUserType.GetType())
> > {
> > case "Person":
> > ((person)currentUserControl).LoadFromForm();
> > ...
> > case "Animal"
> > ((animal)currentUserControl).LoadFromForm();
> > ...
> > }
> >
> > I assume there is something like:
> >
> > ((currentUserControl.GetType())currentUserControl).LoadFromForm();
> >
> > available to call the LoadFromForm method regardless of the user control
> > type, but I can't figure out how to do it.
> >
> > Thanks in advance for any help.
>
>
>
Author
28 Mar 2005 1:17 AM
Byron
I'm currently using an abstract class from which the controls I am interested
in inherit and the methods I'm interested in are public as well as abstract
in that class.  Could I be able to use the technique you suggest in this
case, or do I have to switch to interfaces?

Could you point me to a good source of examples and/or explanations of how
to pull this off?

I am using user controls that I add to a forms control collection to display
them.  These controls all have several methods in common with different
implementations and those methods are overridden from the same abstract
class.  Based on menu selections I want to execute the method in the user
controls within the forms control collection.

In the abstract class:
public abstract void Save(SqlConnection conn);

In the user control that inherits:
public override void Save()
{
....
}

On the main form that contains the control:
private void btnSave_Click(object sender, System.EventArgs e)
{
// How would I pull this off?
this.Controls[i].Save();

//OR as a form field
currentUserControl.Save();
}


Show quote
"Nick Malik [Microsoft]" wrote:

> Create an interface that defines the common methods, and make sure that all
> of your user controls inherit from that method.
> Then you can call your common method without casting.
>
> No need for a switch stmt.  Let Object Oriented development do all the work.
>
>
> --
> --- Nick Malik [Microsoft]
>     MCSD, CFPS, Certified Scrummaster
>     http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
>    I do not answer questions on behalf of my employer.  I'm just a
> programmer helping programmers.
> --
> "Byron" <By***@discussions.microsoft.com> wrote in message
> news:8DF2E57E-560E-427B-B60E-5C07EF323111@microsoft.com...
> >I have several user controls that have a few methods in common, such
> > LoadFromForm() which populates an object from controls on the form.  I
> > want
> > to call that method from the form in which the control is contained
> > regardless of the type currently displayed without having to use a huge
> > switch somthing like:
> >
> > switch(curentUserType.GetType())
> > {
> > case "Person":
> > ((person)currentUserControl).LoadFromForm();
> > ...
> > case "Animal"
> > ((animal)currentUserControl).LoadFromForm();
> > ...
> > }
> >
> > I assume there is something like:
> >
> > ((currentUserControl.GetType())currentUserControl).LoadFromForm();
> >
> > available to call the LoadFromForm method regardless of the user control
> > type, but I can't figure out how to do it.
> >
> > Thanks in advance for any help.
>
>
>
Author
28 Mar 2005 1:19 AM
Byron
I'm currently using an abstract class from which the controls I am interested
in inherit and the methods I'm interested in are public as well as abstract
in that class.  Could I be able to use the technique you suggest in this
case, or do I have to switch to interfaces?

Could you point me to a good source of examples and/or explanations of how
to pull this off?

I am using user controls that I add to a forms control collection to display
them.  These controls all have several methods in common with different
implementations and those methods are overridden from the same abstract
class.  Based on menu selections I want to execute the method in the user
controls within the forms control collection.

In the abstract class:
public abstract void Save(SqlConnection conn);

In the user control that inherits:
public override void Save()
{
....
}

On the main form that contains the control:
private void btnSave_Click(object sender, System.EventArgs e)
{
// How would I pull this off?
this.Controls[i].Save();

//OR as a form field
currentUserControl.Save();
}


Show quote
"Nick Malik [Microsoft]" wrote:

> Create an interface that defines the common methods, and make sure that all
> of your user controls inherit from that method.
> Then you can call your common method without casting.
>
> No need for a switch stmt.  Let Object Oriented development do all the work.
>
>
> --
> --- Nick Malik [Microsoft]
>     MCSD, CFPS, Certified Scrummaster
>     http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
>    I do not answer questions on behalf of my employer.  I'm just a
> programmer helping programmers.
> --
> "Byron" <By***@discussions.microsoft.com> wrote in message
> news:8DF2E57E-560E-427B-B60E-5C07EF323111@microsoft.com...
> >I have several user controls that have a few methods in common, such
> > LoadFromForm() which populates an object from controls on the form.  I
> > want
> > to call that method from the form in which the control is contained
> > regardless of the type currently displayed without having to use a huge
> > switch somthing like:
> >
> > switch(curentUserType.GetType())
> > {
> > case "Person":
> > ((person)currentUserControl).LoadFromForm();
> > ...
> > case "Animal"
> > ((animal)currentUserControl).LoadFromForm();
> > ...
> > }
> >
> > I assume there is something like:
> >
> > ((currentUserControl.GetType())currentUserControl).LoadFromForm();
> >
> > available to call the LoadFromForm method regardless of the user control
> > type, but I can't figure out how to do it.
> >
> > Thanks in advance for any help.
>
>
>
Author
27 Mar 2005 11:43 PM
tribal
also adding to nicks post if the methods contain common
functionality it is better to have a class which inherits
from usercontrol/control and contains these functions


your other user controls can inherit from this class and
the methods will be available regardless of the control.

>-----Original Message-----
>I have several user controls that have a few methods in
common, such
>LoadFromForm() which populates an object from controls on
the form.  I want
>to call that method from the form in which the control is
contained
>regardless of the type currently displayed without having
to use a huge
Show quote
>switch somthing like:
>
>switch(curentUserType.GetType())
>{
>case "Person":
>((person)currentUserControl).LoadFromForm();
>....
>case "Animal"
>((animal)currentUserControl).LoadFromForm();
>....
>}
>
>I assume there is something like:
>
>((currentUserControl.GetType())currentUserControl).LoadFromForm();
>
>available to call the LoadFromForm method regardless of
the user control
Show quote
>type, but I can't figure out how to do it.
>
>Thanks in advance for any help.
>.
>

AddThis Social Bookmark Button