PC Review


Reply
Thread Tools Rate Thread

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

 
 
RichB
Guest
Posts: n/a
 
      26th Jun 2008
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;}.

I have a Person class:

public class Person omain
{
private string name;
private List <Address> addresses;
}

where
class Address omain

In order for IsValid to be true for Person, it also needs to be true for all
addresses.

I therefore within my overridden IsValid Method have a foreach loop:

bool b;
foreach (Domain o in this.addresses)
{
if (o.IsValid)
b = false;
}


This works fine, but each Address has a List<PhoneNumber> phoneNumbers where
PhoneNumberomain. This would use the same loop of code except: foreach
(Domain o in this.phoneNumbers).

Therefore I decided that it would be best to put this functionality into a
Method in the Domain class. Since the Address and PhoneNumber number classes
are both derived from Domain, I thought that I would be able to provide a
method something like:

public IsChildListValid(List<Domain> domainList)
{

}

However List<Address> and List<PhoneNumber> does not cast to List<Domain>.
Is there a way to do this, or are there any other options other than
repeating the code?

Thanks, Richard

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Jun 2008
On Jun 26, 2:11*pm, "RichB" <ri...@community.nospam> wrote:
> 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<Address> and List<PhoneNumber> does 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<T> domainList) where T : Domain
{
...
}

That should work with no problems.

Jon
 
Reply With Quote
 
RichB
Guest
Posts: n/a
 
      26th Jun 2008

Thanks, just what I was after.

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:71412e91-9efb-41e9-b1f0-(E-Mail Removed)...
On Jun 26, 2:11 pm, "RichB" <ri...@community.nospam> wrote:
> 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<Address> and List<PhoneNumber> does 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<T> domainList) where T : Domain
{
...
}

That should work with no problems.

Jon

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cast generic IFoo<T> Alphapage Microsoft C# .NET 15 25th Jul 2008 09:56 PM
Some cast problem and something about generic Tony Microsoft C# .NET 4 18th Jul 2008 08:39 PM
Cast from one generic IDictionary to another pierre.k Microsoft Dot NET 1 16th Jul 2008 02:26 PM
explicit cast between generic types Jon Microsoft C# .NET 8 31st Dec 2007 07:25 PM
generic type and cast Tommaso Caldarola Microsoft C# .NET 1 28th Apr 2006 11:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:27 PM.