Hello everyone,

My problem is that I am creating different objects, and I want them to add themselves to either a List or an ArrayList (difference?) without having to write insects.Add(spider); after each new object I create. How can I add this in the constructor of the insect class?
right now I have the following code for my constructor.

public Insect(int legs, int eyes, string species, string family)
        {
            this.legs=legs;
            ....and so on  
        }

I assume I have to add an ArrayList or List type to my insect class and then add something into my constructor that will add itself to the Array or List?

thank you for the help, just getting back into C# programming

Recommended Answers

All 10 Replies

if you include List in your Insect class, you would just call the Add(T) method to add it:

public class Insect
    {
        List<Insect> insects = new List<Insect>();
        int legs, eyes;
        string species, family;

        public Insect(int legs, int eyes, string species, string family)
        {
            this.legs = legs;
            //....and so on  
            insects.Add(this);
        }
    }

If you inherit the List, then you could do this:

public class Insect : List<Insect>
    {
        //List<Insect> insects = new List<Insect>();
        int legs, eyes;
        string species, family;

        public Insect(int legs, int eyes, string species, string family)
        {
            this.legs = legs;
            //....and so on  
            Add(this); //insects.Add(this);
        }
    }

thank you! that works perfectly.

I cant seem to edit my post... but I'm having a bit of trouble trying to loop through the objects.

So in my insect class I have List<Insect> but I want to some how display all the objects in my Main section. I have a method in insect class that displays the info of the insect.
for example. spider.GetInfo would tell me all about the insect. But I want the method to be able to be called in a foreach loop, display all the details about all the insects. This would require a static method, (so i could call just Insect.GetInfo()) but i have variables in my methods, so that won't work... any help?

I cant seem to edit my post... but I'm having a bit of trouble trying to loop through the objects.

So in my insect class I have List<Insect> but I want to some how display all the objects in my Main section. I have a method in insect class that displays the info of the insect.
for example. spider.GetInfo would tell me all about the insect. But I want the method to be able to be called in a foreach loop, display all the details about all the insects. This would require a static method, (so i could call just Insect.GetInfo()) but i have variables in my methods, so that won't work... any help?

I don't which one you used since you didn't post the code. Here is an example:

foreach (Insect insect in insects)//where insects is your list
    insect.GetInfo();

If that answers your questions, please mark as SOLVED. Otherwise, just ask another question.-- Cheers!

Ah yes my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TESTpractice
{
    class Program
    {
        static void Main(string[] args)
        {
            Program t = new Program();
            t.Run();
        }
        public void Run()
        {
            Insect spider = new Insect(8, 8, "Black Widow", "Arachnida");
            spider.getInfo();

            foreach(Insect insect in insects)
            {
                insect.getInfo();
            }
        }
    }
    public class Insect
    {
        public List<Insect> insects = new List<Insect>();
        public int Eyes { get; set; }
        public int Legs { get; set; }
        public string Species { get; set; }
        public string Family { get; set; }

        public Insect(int myEyes, int myLegs, string mySpecies, string myFamily)
        {
            Eyes = myEyes;
            Legs=myLegs;
            Species = mySpecies;
            Family=myFamily;
            insects.Add(this);
        }
       
        public void getInfo()
        { 
           Console.WriteLine(" Species: {0} \n Family: {1} \n Number of eyes: {2}\n Number of legs: {3}",
           Species, Family, Eyes.ToString(), Legs.ToString());
        }  

    }
}

The name 'insects' does not exist in the current context . Which makes sense, I just can't figure where else to put it. I just want to be able to call all the insects info, or a single one.

Put it inside your Insect class.

Okay, but where? I tried to put it in the getInfo()

public void getInfo()
        {
            foreach (Insect insect in insects)
            {
                Console.WriteLine("Species: {0} \n Family: {1} \n Number of eyes: {2} \n Number of legs: {3}",
           insect.Species, insect.Family, insect.Eyes.ToString(), insect.Legs.ToString());
            }
           
        }

I am only able to call one insect at a time, because I have to declare the object(spider.getInfo) but I would like it so I can just do insect.getInfo, or insect.allInfo, or even just spider.allInfo

Here are some changes I made based on the way you are using your list. NOTE: I didn't compile, but I believe is OK:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TESTpractice
{
    class Program
    {
        static void Main(string[] args)
        {
            Program t = new Program();
            t.Run();
        }
        public void Run()
        {
            Insect insects = new Insect(8, 8, "Ugly Spider", "Arachnida"); // if need to use contsructor
            insects.Add (8, 8, "Black Widow", "Arachnida"); // add method
            insects.Add (8, 8, "Brown Spider", "Arachnida");

            insects.getInfoAll(); // print out entire list
        }
    }
    public class Insect
    {
        public List<Insect> insects = new List<Insect>();
        public int Eyes { get; set; }
        public int Legs { get; set; }
        public string Species { get; set; }
        public string Family { get; set; }

        public Insect()
        {
        }
        public Insect(int myEyes, int myLegs, string mySpecies, string myFamily)
        {
            Add(myEyes, myLegs, mySpecies, myFamily);
        }
        public void Add (int myEyes, int myLegs, string mySpecies, string myFamily)
        {
            Eyes = myEyes;
            Legs = myLegs;
            Species = mySpecies;
            Family = myFamily;
            insects.Add(this);
        }

        public void getInfo()
        {
            Console.WriteLine(" Species: {0} \n Family: {1} \n Number of eyes: {2}\n Number of legs: {3}",
            Species, Family, Eyes.ToString(), Legs.ToString());
        }

        public void getInfoAll()
        {
            foreach (Insect insect in insects)
            {
                insect.getInfo();
            }
        }
    }
}

wow thanks, that is completely different from the way i thought it was going to be. excellent though

Anytime. Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.