Thread: make an array of classes in c#

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    make an array of classes in c#

    Dear Friends,

    how can I make an array of classes in c#?
    please give me a typical syntax. tnx...

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    List instead of array

    I personally prefer to use lists rather than arrays.

    Suppose you have this class:

    Code:
    using System.Drawing;
    
    namespace MyProgram
    {
        public class Car
        {
            public Color Color;
    	public string Make;
            public string Model;
        }
    }
    I would create a list that can hold different cars:
    Code:
    List<Car> NewCars = new List<Car>();
    Then you can create a method that creates your different "cars" and adds them to the list:


    Code:
    private void CreateCar(Color color, string make, string model)
    {
        Car car = new Car();
        car.Color = color;
        car.Make = make;
        car.Model = model;
    
        NewCars.Add(car);
    }
    Hope that helps. Of course, you would be better off setting up a constructor and passing the information that is needed to make that instance of the class, rather than making everything public, but I used this just to demonstrate the idea.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you want to make an array of classes it would be like this:
    Code:
    Car[] carArray = new Car[10];
    
    for(int i = 0;i < carArray.Length;++i)
      carArray[i] = new Car();
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Phyxashun View Post
    I personally prefer to use lists rather than arrays.

    Suppose you have this class:

    Code:
    using System.Drawing;
    
    namespace MyProgram
    {
        public class Car
        {
            public Color Color;
    	public string Make;
            public string Model;
        }
    }
    I would create a list that can hold different cars:
    Code:
    List<Car> NewCars = new List<Car>();
    Then you can create a method that creates your different "cars" and adds them to the list:


    Code:
    private void CreateCar(Color color, string make, string model)
    {
        Car car = new Car();
        car.Color = color;
        car.Make = make;
        car.Model = model;
    
        NewCars.Add(car);
    }
    Hope that helps. Of course, you would be better off setting up a constructor and passing the information that is needed to make that instance of the class, rather than making everything public, but I used this just to demonstrate the idea.
    Thank you for codes,

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by itsme86 View Post
    If you want to make an array of classes it would be like this:
    Code:
    Car[] carArray = new Car[10];
    
    for(int i = 0;i < carArray.Length;++i)
      carArray[i] = new Car();
    Thank you. Your codes raise a question. Does the code" carArray[i] = new Car();" create sub-classes under class car or make objects regarding class car?

    my mean was to save several various classes with different tasks as a array and then using "foreach" to recall them.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by alireza View Post
    Thank you. Your codes raise a question. Does the code" carArray[i] = new Car();" create sub-classes under class car or make objects regarding class car?

    my mean was to save several various classes with different tasks as a array and then using "foreach" to recall them.
    The particular line you are quoting creates a car object and adds it to the array object created in the first line..

    All of the car objects will have the same variables, properties and methods defined in the car class.
    Last edited by Phyxashun; 10-27-2010 at 11:15 AM. Reason: Fixing what I wrote

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thank you for anwsering to me.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by alireza View Post
    my mean was to save several various classes with different tasks as a array and then using "foreach" to recall them.
    If you want to do that, you just need to make sure your array is of a common base type:
    Code:
    Car[] carArray = new Car[10];
    
    carArray[0] = new Ford();
    carArray[1] = new Mustang();
    carArray[2] = new Chevy();
    ...
    If, for instance, you had a Mustang class that derived from a Ford class that derived from a Car class.
    If you understand what you're doing, you're not learning anything.

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I apologize if this is beyond the scope of your question, but how about you use a factory pattern?

    Exploring the Factory Design Pattern
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  2. make array with command line help
    By Brokn in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 05:10 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM