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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 
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.
 
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.Save();

//OR as a form field
currentUserControl.Save();
}
 
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.Save();

//OR as a form field
currentUserControl.Save();
}
 
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.Save();

//OR as a form field
currentUserControl.Save();
}
 
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.Save();

//OR as a form field
currentUserControl.Save();
}
 
Byron said:
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.Save();


((YourAbstractClass)this.Controls).Save();

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

if (Controls is YourAbstractClass)
((YourAbstractClass)this.Controls).Save();
 
Back
Top