Search the web
Sign In
New User? Sign Up
StrongTypes · Free .NET Generics Help
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Casting a SortedList Generic Class to a generic Class of base type.   Message List  
Reply | Forward Message #36 of 44 |
RE: [StrongTypes] Casting a SortedList Generic Class to a generic Class of base type.

I found this very intersting and complicated article about programming with
generics. I hope one of you can benefit from it and understand it because
my head is starting to hurt trying.

http://faculty.cs.tamu.edu/jarvi/papers/cmp_gp.pdf

Santiago Perez
Florida's Turnpike Enterprise
Programmer Analyst

Pompano Operations
Ph 954-975-4855 ex 1127
Cell 954-444-9429



santiago.perez@do
t.state.fl.us
Sent by: To
StrongTypes@yahoo StrongTypes@yahoogroups.com
groups.com cc

Subject
06/06/2006 09:01 RE: [StrongTypes] Casting a
AM SortedList Generic Class to a
generic Class of base type.

Please respond to
StrongTypes@yahoo
groups.com






Just to add to this post Paulo if I may, Dave, what I've mainly seen it
used for is for passing of params to a method. If you have a methods that
needs to perform a Breathe operation on the parameter passed in regardless
of what type it is then you can declare your param as an Interface type and
any obejct passed in that implements it will be valid. A common one back in
the early days of .Net was the IConnection. Am I on track with this Paulo?
This is my true first OOP from scratch and being that it's for the DOT and
will have high exposure I want to make sure I follow the OO rules.

Santiago Perez
Florida's Turnpike Enterprise
Programmer Analyst

Pompano Operations
Ph 954-975-4855 ex 1127
Cell 954-444-9429

Joćo Paulo
Carreiro
<joaoc@programand To
o.net> <StrongTypes@yahoogroups.com>
Sent by: cc
StrongTypes@yahoo
groups.com Subject
RE: [StrongTypes] Casting a
SortedList Generic Class to a
06/06/2006 04:54 generic Class of base type.
AM


Please respond to
StrongTypes@yahoo
groups.com



>Here we've turned a manager into a pet.
You only seem to have broken the laws of nature ( or more specifically the
laws of typing in C#), but in reality you havent'.

If you run your code you'll get a InvalidCastException.

As why would anyone cast to an interface from a class, there are several
reasons. One of the most common, is "to separate interface from
implementation", and it's used on a lot of the GoF design patterns.
http://dofactory.com/Patterns/Patterns.aspx

So the class becomes a "implementation detail", and all on your client code
all you care for is interfaces.

After all, extending your example, if the IMammal interface had a method
"Breath()", then as long as all you wanted is for your objects to breath
you
could use the IMammal interface.

-----Original Message-----
From: StrongTypes@yahoogroups.com [mailto:StrongTypes@yahoogroups.com] On
Behalf Of Dave Cline
Sent: 06 June 2006 02:54
To: StrongTypes@yahoogroups.com
Subject: Re: [StrongTypes] Casting a SortedList Generic Class to a generic
Class of base type.

Paulo,
Ah, well, I stand corrected. Thank you. I realize I was wrong but, is there
a valid reason why someone would want to cast down to an interface?
Essentially turning a person into a painting?

Manager m2 = new Manager();
m2.Name = "Manager M2";

IMammal m3 = m2;

Pet pet3 = (Pet)m3;

Pets.Add(pet3);

Here we've turned a manager into a pet. Which is humorous but certainly
illogical. Yet by using an interface as some intermediary template we've
manage to confound the laws of nature.

Do you have an example of how anyone would do this in a beneficial way?

Dave Cline

On 6/5/06, Joćo Paulo Carreiro <joaoc@...> wrote:
>
> >Trying to cast an object to it's interface well, that would be like
> trying to turn a person into a painting.
> Dave, your anology is wrong. It's perfectly valid to cast an object to
> an interface it supports. Inclusive you don't even need to explicitly
> cast
> it:
>
> IMammal m3 = m2;
>
> Only when going the other way around you need to cast:
> Managers.Add((Manager)m3);
>
> Santiago, the problem you're having is that Generics in C# are
> "invariants".
> A List<Person> is completely unrelated with List<Manager>, although
> Manager inherits from Person.
>
> Maybe this blog post will help you:
> http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx
>
> If all you care is to have a list with the same elements, than you can
> just create a new list and copy the original list in.
>
> List<Person> persons = new List<Person>(Managers.ToArray());
>
> Best Regards,
> Paulo Carreiro
>
>
> -----Original Message-----
> From: StrongTypes@yahoogroups.com <StrongTypes%40yahoogroups.com>
[mailto:
> StrongTypes@yahoogroups.com <StrongTypes%40yahoogroups.com>] On Behalf
> Of santiago.perez@...<santiago.perez%40dot.state.fl.us>
> Sent: 05 June 2006 20:10
> To: StrongTypes@yahoogroups.com <StrongTypes%40yahoogroups.com>
> Subject: Re: [StrongTypes] Casting a SortedList Generic Class to a
> generic Class of base type.
>
> Hey Dave thanks for your help and insight into casting to an
> Interface. I understand what you mean, and I follow your example to
> the T. My only problem is that I want Cast a list of one type to it's
> base which youdidn't have an example of, something like this:
>
> List<Person> Persons =Managers ;
>
> DO I need to specifically cast it as follows?:
> List<Person> Persons =(List<Person>)Managers ;
>
> NO matter what I try I can't cast a list of a sublass to it's base.
> This doesn't seem like something that MS wouldn't have thought about.
>
> Thanks again.
>
> Santiago Perez
> Florida's Turnpike Enterprise
> Programmer Analyst
>
> Pompano Operations
> Ph 954-975-4855 ex 1127
> Cell 954-444-9429
>
> "Dave Cline"
> <davecline@gmail.
> com> To
> Sent by: StrongTypes@yahoogroups.com <StrongTypes%40yahoogroups.com>
> StrongTypes@yahoo cc groups.com Subject
> Re: [StrongTypes] Casting a
> 06/04/2006 06:08 SortedList Generic Class to a PM generic Class of
> base type.
>
>
> Please respond to
> StrongTypes@yahoo
> groups.com
>
>
>
> I'm not expert, in anybody's book, but I believe you're trying to use an
> interface like a base class. An interface is really just a signature, a
> template if you will. It does not represent inheritance. Trying to cast
an
> object to it's interface well, that would be like trying to turn a person
> into a painting. It can't be done without breaking a few laws of nature
> not
> to mention Newtonian physics. But you can turn a person into a child, or
> senior citizen or mammal or vertebrate. A picture of a person is really
> just
> a plan, a image of a three dimensional entity.
>
> Try not using an interface as a generics type. Below was my testing rig.
>
> But I maybe way off here.
>
> DC
>
> //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace InterfaceTesting
> {
> class Program
> {
> static void Main(string[] args)
> {
> System.Console.WriteLine("Testing Inheritance");
>
> Manager m1 = new Manager();
> m1.Name = "Manager M1";
> System.Console.WriteLine("1:" + m1.Name);
>
> Person p1 = (Person)m1;
> System.Console.WriteLine("2:" + p1.Name);
>
> List<Manager> Managers = new List<Manager>();
>
> List<Person> Persons = new List<Person>();
>
> List<Pet> Pets = new List<Pet>();
>
> Manager m2 = new Manager();
> m2.Name = "Manager M2";
>
> Managers.Add(m2);
>
> Persons.Add(m2);
>
> //Pets.Add(m2); // although Pet implements IMammal you can't
> turn a manager into a pet - as much as we'd like to sometimes.
>
> System.Threading.Thread.CurrentThread.Suspend();
> }
> }
>
> interface IMammal
> {
> }
>
> class Pet : IMammal
> {
> public string Name = String.Empty;
> }
>
> class Person : IMammal
> {
> public string Name = String.Empty;
> }
>
> class Manager : Person
> {
> public string Title = String.Empty;
> }
> }
>
>
> Yahoo! Groups Links
>
>
>

--
Dave Cline
www.davecline.com/
davecline@...

[Non-text portions of this message have been removed]

Yahoo! Groups Links










Tue Jun 6, 2006 4:36 pm

saintairbrus...
Offline Offline
Send Email Send Email

Forward
Message #36 of 44 |
Expand Messages Author Sort by Date

Hello everyone, this is my first post so please bare with me. I am trying to understand why I can't cast generic class to a class of a base type. I have the...
Santiago Perez
saintairbrus...
Offline Send Email
Jun 2, 2006
6:46 am

What is the exception or compiler error you are getting? Thank you, Ryan Olshan Website - http://www.StrongTypes.com <http://www.strongtypes.com/> Group -...
Ryan Olshan
teranetlists
Offline Send Email
Jun 2, 2006
7:03 am

Error 2 Cannot implicitly convert type 'ProjectCentral.Contacts.Personnel_List<ProjectCentral.Contacts.Contract_Person>' to ...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 2, 2006
4:23 pm

Talk about simple eh? How you're going to keep all this stuff straight is beyond me. You've got Contact AND Contract all goo'd together and triple inheritance...
Dave Cline
davesmountai...
Online Now Send Email
Jun 2, 2006
4:25 pm

Funny thing is that I did exactly what you're talking about. interface ILetter { } class A : ILetter { private string _ID; public string ID { get { return...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 4, 2006
9:21 pm

I'm not expert, in anybody's book, but I believe you're trying to use an interface like a base class. An interface is really just a signature, a template if...
Dave Cline
davesmountai...
Online Now Send Email
Jun 4, 2006
10:20 pm

Hey Dave thanks for your help and insight into casting to an Interface. I understand what you mean, and I follow your example to the T. My only problem is that...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 5, 2006
9:26 pm

... to turn a person into a painting. Dave, your anology is wrong. It's perfectly valid to cast an object to an interface it supports. Inclusive you don't even...
João Paulo Carreiro
joaopaulocar...
Offline Send Email
Jun 6, 2006
1:31 am

Paulo, Ah, well, I stand corrected. Thank you. I realize I was wrong but, is there a valid reason why someone would want to cast down to an interface? ...
Dave Cline
davesmountai...
Online Now Send Email
Jun 6, 2006
1:54 am

... You only seem to have broken the laws of nature ( or more specifically the laws of typing in C#), but in reality you havent'. If you run your code you'll...
Joćo Paulo Carreiro
joaopaulocar...
Offline Send Email
Jun 6, 2006
8:55 am

Manager m2 = new Manager(); m2.Name = "Manager M2"; IMammal m3 = m2; Mammal m4 = (Mammal)m3; // Generates runtime casting exception interface IMammal { void...
Dave Cline
davesmountai...
Online Now Send Email
Jun 6, 2006
4:14 pm

Just to add to this post Paulo if I may, Dave, what I've mainly seen it used for is for passing of params to a method. If you have a methods that needs to...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 6, 2006
4:32 pm

I found this very intersting and complicated article about programming with generics. I hope one of you can benefit from it and understand it because my head...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 6, 2006
4:42 pm

Ah, that makes sense. Thanks for that. DC On 6/6/06, santiago.perez@... <santiago.perez@...> ... [Non-text portions of this message...
Dave Cline
davesmountai...
Online Now Send Email
Jun 6, 2006
6:25 pm

Paulo, here's another question to further complicate things: Lets say I want to use a sortedlist as the basis, which does not support to Array? I started...
santiago.perez@...
saintairbrus...
Offline Send Email
Jun 6, 2006
4:32 pm

Santiago, Searching on Groups.google.com Generic types are typesafe to the type they are bound to. From the draft of the ECMA spec: <quote> No special...
Dave Cline
davesmountai...
Online Now Send Email
Jun 6, 2006
1:33 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help