Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » [SOLVED] Converting a Generic


Converting a Generic

Closed Thread
  #1 (permalink)  
Old 02-22-11, 06:58 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
Converting a Generic

I have Classes defines as

class A<T> where T:B
class B
class C:B
class D:B

now i have a List<A<B>>

and i want to add a object of A<C> and A<D> to it.

now as both C and D inherit from B i assumed that would work but "Cannot implicitly convert type 'A<C>' to 'A<B>'"

So i tried
Code:
        private List<A<B>> AllB
        {
            get
            {
                List<A<B>> rtn = new List<A<B>>();

                foreach( A<C> c in colOfC){
                    A<C> tmp = c;
                    A<B> tmp2 = (A<B>)tmp;
                    rtn.Add(tmp2);
                }
                foreach(A<D> d in colOfD){
                    A<D> tmp = d;
                    A<B> tmp2 = (A<B>)tmp;
                    rtn.Add(tmp2);
                }
                return rtn;
            }
        }
and it wont have it "Cannot convert type 'A<C>' to 'A<B>'"


any suggestion on how to get this working?
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
  #2 (permalink)  
Old 02-28-11, 08:49 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
Ok i have a solution of sorts now

I've added a method to A that Clones the A object but also casts the C & D to a B during the cloning process
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
matrix display radujit Database 1 01-26-11 12:51 AM
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server altarek Database 5 12-30-10 12:24 AM
Free phpBB Forum Hosting - Jconserv's FFH Network Psan General Advertisements 0 05-13-05 05:30 PM


All times are GMT -5. The time now is 02:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.