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@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;
}
}
[Non-text portions of this message have been removed]
SPONSORED LINKS
Object oriented Object oriented Object oriented
design language
Object oriented Object oriented Object oriented
training methodology tutorial
YAHOO! GROUPS LINKS
Visit your group "StrongTypes" on the web.
To unsubscribe from this group, send an email to:
StrongTypes-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.