Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hey i was here to day playing around with interfaces to try get better to them (cos i never use anything but abstract classes cos i never feel interfaces is much of use) but i have made the following code:

class Program
    {
        static void Main(string[] args)
        {
            List<maintestclass> tl = new List<maintestclass>() { new testclass(), new testclass(), new testclass2()};
            List<testinterface> ti = new List<testinterface>();
 
            Console.ReadLine();
        }
    }
 
    public class testclass : maintestclass, testinterface
    {
        public void testmetode()
        {
            Console.WriteLine("interface call");
        }
    }
 
    public class testclass2 : maintestclass
    {
    }
 
    public abstract class maintestclass
    {
 
    }
 
    public interface testinterface
    {
        void testmetode();
    }

and what i really wanna do is to parse every object in "tl" that can inherit from testinterface to ti but it seems i cant make it to work, i've tried to use "where" and get type but cant find a usefull way to do it...

if anyone fast could help me i would be greatfull. (sample codes would help alot)

- Jackie
Posted 16-May-12 8:36am
Edited 16-May-12 8:37am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If I understood correctly, you can cast an object instance to your interface like:

testinterface someVar = originalVar as testinterface;
after that someVar should contain the variable as your interface if the original instance implemented the interface. If it didn't, someVar is null.
  Permalink  
Comments
Jackssen at 16-May-12 13:53pm
   
Thanks is there a way to do this by using .where on a list?
lewax00 at 16-May-12 14:16pm
   
Something like this?
Where(x => (x as testinterface) != null)
Mika Wendelius at 16-May-12 14:28pm
   
Yep, just like that :)
Jackssen at 16-May-12 14:39pm
   
keep getting "Error: Cannot convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'"

this code is the problem: ti = (List)tl.Where(x => (x as testinterface) != null).ToList();

Thanks alot for helping :)
Mika Wendelius at 16-May-12 14:44pm
   
How are ti and tl defined?
Jackssen at 16-May-12 14:46pm
   
as following: (see source above for details):
List(maintestclass) tl = new List(maintestclass)() { new testclass("test"), new testclass("test"), new testclass2("test") };

List(testinterface) ti = new List(testinterface)();

those greater/lesser thing wont work it takes it as HTML...
Mika Wendelius at 16-May-12 14:56pm
   
Ok, what if you try something like this:

System.Collections.Generic.List<object> tl = new System.Collections.Generic.List<object>();

tl.Add(new testclass("test"));

tl.Add(new testclass("test"));

tl.Add(new testclass2("test"));
System.Collections.Generic.List<testinterface> ti = new System.Collections.Generic.List<testinterface>();



ti = tl.Where(x => (x as testinterface) != null).OfType<testinterface>().ToList();
Jackssen at 16-May-12 15:02pm
   
The problem with this is that tl MOST be list of maintestclass, sorry...
Jackssen at 16-May-12 15:05pm
   
WOW! made it work just using the "oftype" thanks a lot :)
Mika Wendelius at 16-May-12 15:10pm
   
Glad it got solved :)
Jackssen at 16-May-12 15:14pm
   
its silly you cant cast it as i tried... but lists of generic types have always been a pain... i wished i've seen oftype earlier haha :D anyway thanks again +5 from here for great help chat and support have a nice day/evening :)
Mika Wendelius at 16-May-12 15:16pm
   
Thanks and the same to you :)
lewax00 at 16-May-12 14:52pm
   
Why are you converting a list into a list?

ti = tl.Where(x => (x as testinterface) != null).ToList();

Should work fine...
Jackssen at 16-May-12 14:55pm
   
almost same error as above with that code :)

can not implicitly convert bla bla...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,554
1 OriginalGriff 6,258
2 Peter Leow 2,534
3 Maciej Los 2,308
4 Abhinav S 2,274


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 16 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100