472,956 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 software developers and data experts.

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 2188
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
10
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
1
by: moondaddy | last post by:
I need to replace a user control on a page with a different user control. The challenge I'm faced with is that this call is being made from the user control being replaced. Normally I replace...
4
by: Zuel | last post by:
Hi Folks. So I have a small problem. My DoPostBack function is not writen to the HTML page nor are the asp:buttons calling the DoPostBack. My Goal is to create a totaly dynamic web page where...
7
by: J Smithers | last post by:
I have several ASPX pages (with code-behind logic) that I reuse amongst many Web sites on the same production server. Currently each Web site has its own copy of these aspx pages. I was thinking...
1
by: keith.walter | last post by:
My first asp.net app is almost "done" and I am stuck. Here is my situation: I have a "mother" page add_customer.aspx and a"child" user control add_group.ascx. On the mother page is an "add...
3
by: John E. | last post by:
I am trying to find a way to not have to reference an object in all my projects, since it is initialized & instantiated in my Common class. I have a 4 tier project (presentation, rules, dal,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.