473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2211
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.