468,249 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

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.
Nov 17 '05 #1
7 2040
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:8D**********************************@microsof t.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.

Nov 17 '05 #2

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, suchLoadFromForm() which populates an object from controls on the form. I wantto call that method from the form in which the control is containedregardless of the type currently displayed without having to use a hugeswitch 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 controltype, but I can't figure out how to do it.

Thanks in advance for any help.
.

Nov 17 '05 #3
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();
}


"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:8D**********************************@microsof t.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.


Nov 17 '05 #4
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();
}
"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:8D**********************************@microsof t.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.


Nov 17 '05 #5
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();
}


"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:8D**********************************@microsof t.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.


Nov 17 '05 #6
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();
}
"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:8D**********************************@microsof t.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.


Nov 17 '05 #7
"Byron" <By***@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.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
ca*****@tamu.edu
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by J Smithers | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.