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...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

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 #24 of 44 |
RE: [StrongTypes] Casting a SortedList Generic Class to a generic Class of base type.

Error 2 Cannot implicitly convert type
'ProjectCentral.Contacts.Personnel_List<ProjectCentral.Contacts.Contract_Person>\
'
to
'ProjectCentral.Contacts.Personnel_List<ProjectCentral.Contacts.iPerson>'
C:\Inetpub\wwwroot\ProjectCentral\Contracts\ContractContacts.aspx.cs 43
40 http://localhost/ProjectCentral/

UcPersonnelList1.DataSource = ((Contract)this
.ContractDetail1.DataSource).Personnel_List;


UcPersonnelList1.DataSource is defined as
public Personnel_List<iPerson> DataSource { get; set; }


and Contract.Personnel_List is:
public ProjectCentral.Contacts.Personnel_List<Contract_Person>
Personnel_List
{
get
{ if (this._Personnel_List == null)
this._Personnel_List = new Personnel_List<
Contract_Person >();

return this._Personnel_List;
}
set
{
this.Personnel_List = value;
}
}




Santiago Perez
Florida's Turnpike Enterprise
Programmer Analyst

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



"Ryan Olshan"
<teranetlists@ter
anetsystems.com> To
Sent by: <StrongTypes@yahoogroups.com>
StrongTypes@yahoo cc
groups.com
Subject
RE: [StrongTypes] Casting a
06/02/2006 02:59 SortedList Generic Class to a
AM generic Class of base type.


Please respond to
StrongTypes@yahoo
groups.com






What is the exception or compiler error you are getting?

Thank you,
Ryan Olshan
Website - http://www.StrongTypes.com <http://www.strongtypes.com/>
Group - http://groups.yahoo.com/group/StrongTypes
Blog - http://blogs.strongcoders.com/blogs/ryan/


_____

From: StrongTypes@yahoogroups.com [mailto:StrongTypes@yahoogroups.com] On
Behalf Of Santiago Perez
Sent: Thursday, June 01, 2006 11:10 AM
To: StrongTypes@yahoogroups.com
Subject: [StrongTypes] Casting a SortedList Generic Class to a generic
Class
of base type.


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 following setup:
///////////////////////////////////////////////////////////////////
public class Personnel_List<T> :
ProjectCentral.GlobalObjects.PersistentObject_List<Person_Abs> where
T : Person_Abs, iPerson
{
}


public abstract class Person_Abs :
ProjectCentral.GlobalObjects.PersistentObject, Contacts.iPerson
{
}


public class Vendor_Person : Person
{
}

public class Contract_Person : Vendor_Person
{
////////////////////////////////////////////////


Ok so now I have a contract class that has a property Personnel_List
which is

/////////////////////////////////////////////////////////////
public class Contract :
ProjectCentral.GlobalObjects.PersistentObject, iContract
{
public
ProjectCentral.Contacts.Personnel_List<Contacts.Contract_Person>
Personnel_List
{
get
{ if (this._Personnel_List == null)
this._Personnel_List = new
Personnel_List<Contract_Person>();

return this._Personnel_List;
}
set
{
this.Personnel_List = value;
}
}

}
//////////////////////////////////////////////////////////////////


THen I have a usercontrol that I want to use everywhere to list
Personnel so I set up a property as follows:


//////////////////////////////////////////////////////////
public Personnel_List<Person> DataSource
{
get { return (Personnel_List<Person>)
grdPersonnel.DataSource; }
set {grdPersonnel.DataSource = value;}
}
////////////////////////////////////////////////////////////


So when I try to do this it bombs on me:
///////////////////////////////////////////////////////////////
UcPersonnelList1.DataSource = ((Contract)
this.ContractDetail1.DataSource).Personnel_List;
////////////////////////////////////////////////////////////


Even if I try this:
///////////////////////////////////////////////////////////////
UcPersonnelList1.DataSource = (Personnel_List<Person>)((Contract)
this.ContractDetail1.DataSource).Personnel_List;
////////////////////////////////////////////////////////////



ANy help would be greatly appreciated. My boss is starting to look
at me like I don't know what I'm doing.












SPONSORED LINKS
Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented&w1=Object+oriented&w2=O
bject+oriented+design&w3=Object+oriented+language&w4=Object+oriented+trainin

g&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=172&.sig=

uLqg2pfGRplGmMsp6kJtVQ> oriented Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented+design&w1=Object+orient
ed&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriented+

training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=17

2&.sig=pGWYLp0sQpJs3JoBMlqbdA> oriented design Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented+language&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente

d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=

172&.sig=oMxENFA3eiv-nKTwqk3z6Q> oriented language
Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented+training&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente

d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=

172&.sig=WW87rn12Wb695fEwKFWRNg> oriented training Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented+methodology&w1=Object+o
riented&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+orie

nted+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6

&s=172&.sig=ZfydFONc8Llu8PBIVAyRuQ> oriented methodology Object
<
http://groups.yahoo.com/gads?t=ms&k=Object+oriented+tutorial&w1=Object+orie
nted&w2=Object+oriented+design&w3=Object+oriented+language&w4=Object+oriente

d+training&w5=Object+oriented+methodology&w6=Object+oriented+tutorial&c=6&s=

172&.sig=5RlQ7dXcE52DzP8Rq1Dbdg> oriented tutorial

_____

YAHOO! GROUPS LINKS



* Visit your group "StrongTypes
<http://groups.yahoo.com/group/StrongTypes> " on the web.


* To unsubscribe from this group, send an email to:
StrongTypes-unsubscribe@yahoogroups.com
<mailto:StrongTypes-unsubscribe@yahoogroups.com?subject=Unsubscribe>


* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .


_____




[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.









Fri Jun 2, 2006 3:59 pm

saintairbrus...
Offline Offline
Send Email Send Email

Forward
Message #24 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