I have a quesions

Last post 07-05-2009 4:51 AM by anders__f. 9 replies.

Sort Posts:

  • I have a quesions

    07-02-2009, 9:11 AM
    • Member
      40 point Member
    • abdelnaby
    • Member since 07-02-2009, 1:07 PM
    • Posts 8

    Dear All

    this is my first post in this forum

    i have an issue i am trying to solve it since about 3 monthes

    I got a type in a Type variable

    Type T ;

    I have a List of objects

    List<Object> mList ;

    i want to case mList as a List<T>

    is there any solution ?

     

    THanks in advance

     

     

     

     

     

    Mohamed Abdel Naby
    Senior asp.net developer
    Egypt
  • Re: I have a quesions

    07-02-2009, 10:56 AM
    • Participant
      1,470 point Participant
    • Heloril
    • Member since 10-30-2007, 1:38 PM
    • Belgium
    • Posts 255

    Hi,

    To cast two lists use this static class:

    public class ListConverter<TFrom, TTo>
        where TFrom : TTo
        {
            public static IEnumerable<TTo> Convert(IEnumerable<TFrom> from)
            { return Convert(new List<TFrom>(from)); }
            public static List<TTo> Convert(List<TFrom> from)
            { return from.ConvertAll<TTo>(new Converter<TFrom, TTo>(Convert)); }
    
            public static TTo Convert(TFrom from) { return (TTo)from; }
        }


     

    And to use it:

    private List<AttachedItem> GetAttachedFiles(int issueEventID)
            {
                return ListConverter<AttachedFile, AttachedItem>.Convert(attachedFileDAL.GetEventAttachedFiles(issueEventID));
            }
    
            private List<AttachedItem> GetDispatches(int issueEventID)
            {
                return ListConverter<Dispatch, AttachedItem>.Convert(dispatchDAL.GetDispatches(issueEventID));
            }

    Here AttachedFile and Dispatch inherits from AttachedItem.

     

    Mark as Answer if this reply helps you

    My blog: http://www.notesfor.net/
  • Re: I have a quesions

    07-02-2009, 1:43 PM
    • Member
      40 point Member
    • abdelnaby
    • Member since 07-02-2009, 1:07 PM
    • Posts 8

     Dear

    Thanks for your reply

    unfortunatily there is 2 problems

    1- your clas does not accept object as first type

    2- your class does not accpt type variable as second type

     

    here is example of the code i want to run

    <object> lo = new List<object>();

    lo.add(new Rectangle ( 0 , 0 , 50 , 50 ));

    lo.add(new Rectangle ( 0 , 0 , 50 , 50 ));

    Type T = typeof ( Rectangle) ;

    ListConverter < object , T>.Convert ( lo );

    Mohamed Abdel Naby
    Senior asp.net developer
    Egypt
  • Re: I have a quesions

    07-02-2009, 2:49 PM
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 6:44 AM
    • Sweden
    • Posts 34

    If the objects you add to your "lo" really is of type Rectangle, the easiest way is to just cast them. With Linq you can do this very easy:

    private static void Test()
    {
        List<object> lo = new List<object>();
        lo.Add(new Rectangle(1, 0, 50, 50));
        lo.Add(new Rectangle(2, 0, 50, 50));
        List<Rectangle> converted = lo.Cast<Rectangle>().ToList();
    }


  • Re: I have a quesions

    07-02-2009, 3:51 PM
    • Member
      40 point Member
    • abdelnaby
    • Member since 07-02-2009, 1:07 PM
    • Posts 8

     Rectangle is just an example

    Actually i dont have a defind type like rectangle

    I have the destination type stored in a type variable

    Consider this code

    void ProcessTTypeList( List<Object> T , Type mType )

    {

     lo.add(new Rectangle ( 0 , 0 , 50 , 50 ));

    lo.add(new Rectangle ( 0 , 0 , 50 , 50 ));

    ListConverter < object , mType>.Convert ( lo ); /* this is what i rally need */

    }

    Mohamed Abdel Naby
    Senior asp.net developer
    Egypt
  • Re: I have a quesions

    07-03-2009, 5:52 AM
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 6:44 AM
    • Sweden
    • Posts 34

    Well, I don't think that's possible. Probably the best thing to do is to change the way you think. Maybe you are trying to solve the wrong problem.

    Why do you use a List<object>, and what do you want to do with the converted list?

  • Re: I have a quesions

    07-04-2009, 5:23 AM
    • All-Star
      61,743 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,094
    • TrustedFriends-MVPs
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: I have a quesions

    07-04-2009, 10:02 AM
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 6:44 AM
    • Sweden
    • Posts 34

    TATWORTH, you don't link to a specific forum thread, and I don't know why that forum should be better than this. What we need to know to help is more about what the OP wants to achieve.

    Maybe the best thing would be to use an interface for the types that could possibly be in the list.

  • Re: I have a quesions

    07-04-2009, 11:16 AM
    • All-Star
      61,743 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,094
    • TrustedFriends-MVPs

    anders__f:
    TATWORTH, you don't link to a specific forum thread, and I don't know why that forum should be better than this

    Whilst this is a very good forum for ASP.NET questions, there are certain questions which while they can be legitimatly asked on this forum, they could also be asked elsewhere. The MSDN Dot Net forum seemed the best other forum for the question to be asked.

    anders__f:
    Maybe the best thing would be to use an interface for the types that could possibly be in the list.

    Good point! May I respectfully suggest that you either find an example elsewhere on the web or post a sample yourself.

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: I have a quesions

    07-05-2009, 4:51 AM
    Answer
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 6:44 AM
    • Sweden
    • Posts 34

    I'm sure that there is a number of forums where this question could be asked, some of them maybe better suited than this forum. But my point is that in this thread on this forum I've already started to help, and there is definitely a number of other users on this forum that would be able to answer this question too.

    The thing is, to be able to help the OP, we need some more description about the problem and what he wants to achieve. Sorry, but I can't see the point in trying another forum in this situation.

    Anyway, here is a simple sample code showing how you could use an interface for the types that is to be added to the list. To use the methods or properties defined in the interface there is no need to cast the list.

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Rectangles();
                Circles();
                BothRectanglesAndCircles();
            }
    
            private static void Rectangles()
            {
                var list = new List<IGeometrical>
                               {
                                   new Rectangle(0, 0, 50, 50),
                                   new Rectangle(10, 10, 40, 40)
                               };
                DisplayArea(list);
            }
    
            private static void Circles()
            {
                var list = new List<IGeometrical>
                               {
                                   new Circle(0, 0, 15),
                                   new Circle(15, 15, 15)
                               };
                DisplayArea(list);
            }
    
            private static void BothRectanglesAndCircles()
            {
                var list = new List<IGeometrical>
                               {
                                   new Rectangle(20, 20, 30, 10),
                                   new Circle(15, 15, 45)
                               };
                DisplayArea(list);
            }
    
            private static void DisplayArea(List<IGeometrical> list)
            {
                foreach (var geometrical in list)
                {
                    Console.WriteLine("{0}: {1}",
                        geometrical.Name,
                        geometrical.GetArea());
                }
            }
        }
    
        public interface IGeometrical
        {
            string Name { get; }
            double GetArea();
        }
    
        public class Rectangle : IGeometrical
        {
            public int X { get; set; }
            public int Y { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
    
            public Rectangle(int x, int y, int width, int height)
            {
                X = x;
                Y = y;
                Width = width;
                Height = height;
            }
    
            public string Name
            {
                get { return "Rectangle"; }
            }
    
            public double GetArea()
            {
                return Width * Height;
            }
        }
    
        public class Circle : IGeometrical
        {
            public int X { get; set; }
            public int Y { get; set; }
            public int Radius { get; set; }
    
            public Circle(int x, int y, int radius)
            {
                X = x;
                Y = y;
                Radius = radius;
            }
    
            public string Name
            {
                get { return "Circle"; }
            }
    
            public double GetArea()
            {
                return Math.PI * Radius * Radius;
            }
        }
    }
    


Page 1 of 1 (10 items)