473,387 Members | 1,771 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.

Up Casting a Control Collection

Hi,

I'm trying to write a general function to simply clear a collection of
fields from both a group box and a tab control. Here is what I want my code
to look like:

private void ClearFields(GroupBox groupbox)
{
ClearFields((ControlCollection)groupbox.Controls);
}

private void ClearFields(TabPage pg)
{
ClearFields((ControlCollection)pg.Controls);
}

private void ClearFields(ControlCollection lst)
{
foreach (Control c in lst)
{
if (c.GetType() != typeof(System.Windows.Forms.Button))
{
c.Text = "";
}
}
}
}

However, when I write my code this way I get a runtime error that says I
cannot convert the type TabPageControlCollection to a ControlCollection.
TabPageControlCollection is a derived class of ControlCollection. What is it
that I'm not understanding about casting?
Jan 4 '08 #1
2 2635
On Fri, 04 Jan 2008 13:09:01 -0800, Trevor
<Tr****@discussions.microsoft.comwrote:
[...]
However, when I write my code this way I get a runtime error that says I
cannot convert the type TabPageControlCollection to a ControlCollection.
TabPageControlCollection is a derived class of ControlCollection. What
is it
that I'm not understanding about casting?
I'm not sure. The code doesn't look obviously wrong to me.

That said, there should be no need for an explicit cast. The upcast
should happen automatically, assuming you're actually using the right
types for the objects.

The first question is what is the _exact_ error message you get. It
should provide fully-qualified names for the types involved in the
cast...what are those names? Chances are, at least one of them is not the
type you thought it would be. This is usually a namespace problem, where
one type came from a different namespace than the other and thus in fact
isn't related to the other.

But without knowing the exact, complete text of the error and without a
concise-but-complete code sample that reliably reproduces the problem,
it's hard to know for sure.

Pete
Jan 4 '08 #2
Pete,

I was typing a long, reply message when I hovered over the signature of the
ClearFields(ControlCollection lst) function and discovered that the namespace
there was System.Windows.Forms.FORM.ControlCollection instead of
System.Windows.Forms.Control.ControlCollection.

Subtle, but you were correct. Thank you. No explicit cast needed, and my
faith in my understanding of overloading in c sharp is restored.

Trevor
Jan 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MattC | last post by:
Hi, How can do runtime casting? MyCollection derives from ArrayList I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the...
3
by: Barry Mossman | last post by:
Hi, I get the feeling that I am missing something with regards to casting. The CopyTo method allows me to copy the contents of a collection into an array. My collection is a MatchCollection...
4
by: Steven Wolf | last post by:
Hi folks, i developing right now a big project for my company and i have follow issue: I have a base class for all my domain objects (abstract DomainObject) and many objects derived from that...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
5
by: Mac via DotNetMonster.com | last post by:
Hi all, I have a creating a my own tabpage class (MyTabPage) which inherits the .Net TabPage class. In the relevant event I want to loop through the collection of TabPages and then when I...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
2
by: cylt | last post by:
Hi, I would like to have something like that : IList<IUser> list2 = list1 as IList<IUser>; where list1 is a generic collection of User ( IList list1<User>=new List<User>() ) and where User...
1
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.