Thread: Create instance on class programatically

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    Create instance on class programatically

    what is the correct procedure to automate instantiation of a class ?

    if i create a class
    Code:
    class animal()
    {
    int age { get; set; }   // private by default
    int weight { get; set; }
    int counter { get; set; }
    
    public animal() //default constructor
    {
    age = 20;
    weight = 10;
    }
    
    public void set_age(int new_age)
    {
    age = new_age;
    }
    
    public void set_weight(int new_weight)
    {
    weight = new_weight;
    }
    now i want to create an infinite number of animals in my code.

    i figure it has to use an array to track objects

    animal[i].set_age(int 30);
    animal[i].set_weight(int 40);

    but how do i implement this so i instantiate a new animal anywhere and the class will increment its index and create it ( and keep count so i can check) ?

    i have seen this somewhere but think i lost the link

    cheers

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Code:
    List<Animal> animals = new List<Animal>();
    for (int i = 0; i < 1000; ++i)
    {
      animals.Add(new Animal());
    }
    for (int i = 0; i < 1000; ++i)
    {
      animals[i].set_age(30);
      animals[i].set_weight(30);
    }

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    There are many options. You can create an normal array or you can use an list
    Code:
    animal [] myAnimalArray = new animal[5];
    for(int i = 0; i < myAnimalArray.length; i++)
    {
       myAnimalArray[i] = new animal();
    }//for
    Code:
    System.Collections.Generic.List<animal> myList = new System.Collections.Generic.List<animal>();
    for (int i = 0; i < 5; i++)
    {
        myList.Add(new animal());
    }//for
    
    int count = myList.Count;
    Woop?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thanks guys but both of these rely on having a predetermined number to start.

    i < 1000
    new animal[5]

    but i want to be able to jst stick in my code

    new_animal()

    and have the code do something along the lines of

    Code:
    public partial class Form1 : Form
        {
            Person mypeople = new Person();
            Person []newpeople;
    
            public Form1()
            {
                InitializeComponent();
                mypeople.new_person(newpeople);
            }
    }
    Code:
    namespace Class_test
    {
        public class Person
        {
            private string name { get; set; }
            private string lang { get; set; }
            private int age { get; set; }
            private int person_count { get; set; }
    
            public Person()
            {
                name = "james";
                lang = "english";
                age = 25;
                MessageBox.Show("constructor called");
            }
    
            public Person new_person(Person []newperson)
            { 
            newperson[new_person++] = new Person(); //<-- creates instance here but returns a null reference
    
            return newperson[0];
            }
    }
    i have seen this done in some code where they called a constructor to increment a count then created an instance of their class from the value of that count.

    i cant remember whether it all happened inside the class, or 2 seperate classes/constructors tbh.

    }
    }

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    first of all, the code you posted is invalid syntax. you can't increment a method, nor can you increment an array.

    second, using a List<> allows you to have as many animals as you'd like assuming you have the available memory.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    Code:
    namespace Class_test
    {
        public partial class Form1 : Form
        {
            int[]newpeople = new int[10];
            int I = 0;
    
            public Form1()
            {
                InitializeComponent();
                
                newpeople[I++] = 10;
                newpeople[I++] = 20;
                newpeople[I++] = 30;
            }
        }
    }
    is it just the way i have the previous code - because the array increment works fine ?

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Code:
    List<Animal> animals = new List<Animal>();
    
    // ... Somewhere else in our code ...
    
    Animal temp = new Animal();
    temp.set_age(30);
    animals.Add(temp);
    In that way, you can add however many animals you want, just make a new animal and call .Add() to put it in the list.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. pointer to an instance of a different class
    By xlix in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2003, 04:19 PM
  4. Class function that returns the instance it's called from
    By L Boksha in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2002, 11:52 AM