473,385 Members | 1,838 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,385 software developers and data experts.

Re: Can I cast a generic list, or do I need another approach

On Jun 26, 2:11*pm, "RichB" <ri...@community.nospamwrote:
I have an abstract class Domain which is responsible for validation of my
model. All classes in the model inherit from Domain for operations such as
bool IsValid {get;}.
<snip>
However List<Addressand List<PhoneNumberdoes not cast to List<Domain>..
Is there a way to do this, or are there any other options other than
repeating the code?
Yes, there's a way to do it - you need to make the method generic:

public bool IsChildListValid<T>(List<TdomainList) where T : Domain
{
...
}

That should work with no problems.

Jon
Jun 27 '08 #1
1 1241

Thanks, just what I was after.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:71**********************************@56g2000h sm.googlegroups.com...
On Jun 26, 2:11 pm, "RichB" <ri...@community.nospamwrote:
I have an abstract class Domain which is responsible for validation of my
model. All classes in the model inherit from Domain for operations such as
bool IsValid {get;}.
<snip>
However List<Addressand List<PhoneNumberdoes not cast to List<Domain>.
Is there a way to do this, or are there any other options other than
repeating the code?
Yes, there's a way to do it - you need to make the method generic:

public bool IsChildListValid<T>(List<TdomainList) where T : Domain
{
...
}

That should work with no problems.

Jon

Jun 27 '08 #2

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

Similar topics

0
by: | last post by:
We have an object factory that returns weakly typed datasets (just plain ol' DataSet's). I need to cast it, or transform it, or map it to a strongly typed dataset. There's an old adage I heard...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
4
by: colson | last post by:
Hi, If I have a class A, and a List<List<object>> containing instances of A. How do I explicitly cast List<List<object>> as List<List<A>>?
7
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I have a Class "Multidictionary" which I created which is basically a dictionary which can have multiple values for the same key. The class centers around private Dictionary<K,...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then...
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: 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
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?
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
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
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.