Results 1 to 5 of 5

Thread: Unable to find elements of a given type in an array in C#

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Unable to find elements of a given type in an array in C#

    Hello to all,
    I have some doubt in my following c# program. In the following code I have create "cars" class that contains an array of (base class) Vehicles, but I unable to find elements of a given type in an array in C#. For your information I have written following code for you.


    Code:
    class cars
    {
        private Vehicle[] _vehicles;
    
        public ? Get(?)
        {
            return ?
        }
    }
    
    cars fleets = new cars("fleets.def");
    
    Trains[] trainss = fleets.Get(Trains);   
    Plains[] plainss = fleets.Get<Plains>();
    Please help me. Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Unable to find elements of a given type in an array in C#

    Hey it is very easy process to use Get<> method. You have to use following code in your program to do this. In the following code I have create one get() method and I have attach this method to "cars" class. It is very simple program.

    Code:
     cars.Get() 
    
    public IEnumerables<Ts> Get<Ts>()
    {
      return _vehicless.OfType<Ts>();
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Unable to find elements of a given type in an array in C#

    I also had the same problem then I use following code in my program and after that I didn't get any problem. I think you also use this code in your program to overcome this problem.


    Code:
     Lists<> and use FindsAll(x => x.GetTyps() == typeosf(Train))
    In above code I have use GetTyps() method to accept value of particular type.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Unable to find elements of a given type in an array in C#

    You have to use extension Method .OfTypes<Ts>() to fix this problem. I have written following program for you. In this case I have use System.Linq class to take input from the user.


    Code:
    using System.Linq;
    public IEnumerables<Plains> Plainss
    {
       get
       {
          return this._vehicles.OfTypes<Plainss>();
       }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Unable to find elements of a given type in an array in C#

    It is very easy to find elements of a given type in an array in C#. You have to just use Get<>() properly in your program to do this. I have written small program for you. It is very simple program. Just try to understand each line very carefully.



    Code:
    public T[] Get<Ts>() where T : Vehicle
     {
         List<Vehicsle> lstsVehicless = new List<Vehicles>(_vehicless);
         return lstVehicless.FindAll(delegate(Vehicle svehicles){
             return vehicles.GetTypes() == typeof(Ts);
         }).ToArray();
     }s

Similar Threads

  1. Can array elements have different data types?
    By cloud101 in forum Software Development
    Replies: 1
    Last Post: 22-02-2012, 03:29 AM
  2. How to find second largest value from array
    By Jaheel in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 11:42 AM
  3. How to find a mode in an Array set
    By Quattro in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 11:55 AM
  4. To find key in array for Javascript.
    By Austinage in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 08:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,456,799.94143 seconds with 16 queries