dashinash
dashinash

Reputation: 11

Why am I getting a StackOverflowException?

class Program
{
    static void Main(string[] args)
    {
        //Declare Variables
        string name;
        string breed;
        string colour;
        string gender;
        string userChoice;
        bool proceed;

        //Initialize Variables
        name = "";
        breed = "";
        colour = "";
        gender = "";
        proceed = true;
        numberOfDogs = 0;

        Dogs dogList = new Dogs();

        do
        {
            Console.Write("Main Menu\n");
            Console.WriteLine("--------------------------------------------------------");
            Console.WriteLine("1.   Display the name, breed, colour, and gender of the dog (if one already exists)");
            Console.WriteLine("2.   Add a new dog");
            Console.WriteLine("3.   Edit an existing dog");
            Console.WriteLine("4.   Exit the program");
            userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "A":
                case "a":
                    {
                        dogList.Display();
                        break;
                    }
                case "B":
                case "b":
                    {
                        Console.Clear();
                        Console.Write("Please enter the dog's name: ");
                        name = Console.ReadLine();
                        Console.Write("Please enter the breed of the dog: ");
                        breed = Console.ReadLine();
                        Console.Write("Please enter the colour of the dog: ");
                        colour = Console.ReadLine();
                        Console.Write("Please enter the dog's gender: ");
                        gender = Console.ReadLine();
                        dogList = new Dogs(name, breed, colour, gender);
                        break;
                    }
                case "C":
                case "c":
                    {
                        dogList.Display();
                        dogList = new Dogs();
                        break;
                    }
                case "D":
                case "d":
                    {
                        proceed = false;
                        break;
                    }
                }
            } while (proceed);
        } 
    }
}

I had another code that I was using but my teacher wanted me to have it in this set up. When I ran her example it said the same thing... Does anyone know what the problem is?

I have looked it over multiple times and I can't seem to figure it out. I am a total beginner for coding so it is sometimes really hard to troubleshoot issues... I have emailed the teacher to let her know about the example, but she has not gotten back to me yet. If someone could help me out, I would greatly appreciate it!

class Dogs
{
    //Declare Dog Variables
    string name;
    string breed;
    string colour;
    string gender;

    public Dogs()
    {
        name = null;
        breed = null;
        colour = null;
        gender = null;
        Dogs dogList = new Dogs();
    }
    public Dogs(string dogName, string dogBreed, string dogColour, string dogGender)
    {
        name = dogName;
        breed = dogBreed;
        colour = dogColour;
        gender = dogGender;
    }
    public void Display()
    {
        if (name == null || breed == null || colour == null || gender == null)
        {
            Console.Write("No dog record exists");
        }
        else
        {
            Console.WriteLine("The dogs name is: " + name);
            Console.WriteLine("The breed of the dog is: " + breed);
            Console.WriteLine("The colour of the dog is: " + colour);
            Console.WriteLine("The dogs gender is: " + gender);
        }
    }
}

}

Upvotes: 0

Views: 59

Answers (1)

Arsen Khachaturyan
Arsen Khachaturyan

Reputation: 8330

With these lines of code:

class Dogs
{ 
    public Dogs()
    {
        // ...
        Dogs dogList = new Dogs();
    }
}

you are creating recursive call to the same Dog constructor, which will continue till there will be no more available memory allocated for your C# program.

When this happens, StackOverflow exception will be thrown.


To achieve a similar behavior, you can do this:

public interface IDog
{
}

public class Dog : IDog
{
    public List<IDog> DogsList { get; set; }

    public string Name { get; set; }

    public Dog()
    {
        // other initialization logic here
    }
}

and later use it following way:

var dog = new Dog
{
    Name = "Scooby",
    DogsList = new List<IDog>
    {
        {new Dog() {Name = "Do"}}
    }
};

Console.WriteLine($"Main dog={dog.Name}");

foreach (var dog1 in dog.DogsList)
{
    Console.WriteLine($"Living together with: {dog.Name}");
}

or even simpler, just have single Dog class and create a list of Dogs later:

public class Dog { 
}

...

var dogList = new List<Dog>();

Upvotes: 1

Related Questions