Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Stack overflow. Repeat 261929 times[…]" is the error message when I have debugged my code then I do not see any issues so what should I do?

I have coded every line with appropriate things, however, I received an error when I tested after selecting one of the five options from the menu which is prompting the user to choose it for register dog and so on. Therefore, I am unable to recognize any wrong with my code yet I did debug and tested it many times. I do not know what to do with it even though I checked to ensure every case is not overlapped without having a "break;".

Repeat 261929 times:
--------------------------------
   at Dog.set_Name(System.String)
--------------------------------
   at Dog..ctor(System.String, System.String, Int32, System.String)
   at Program.<Main>$(System.String[])

Class:

public class Dog
{
    public string Name { get
    {
        return Name;
    }
    set
    {
        Name = value;
    }
    }
    public string Breed { get
    {
        return Breed;
    }
    set
    {
        Breed = value;
    }
    }
    public int Age { get
    {
        return Age;
    }
    set
    {
        Age = value;
    }
    }
    public string Vaccine { get
    {
        return Vaccine;
    }
    set
    {
        Vaccine = value;
    }
    }

    //default constructor
    public Dog()
    {
        Name = "N/A";
        Breed = "N/A";
        Age = -1;
        string [] vaccine = new string[10];
    }

    //parameterized constructor
    public Dog(string name, string breed, int age, string vaccine)
    {
        Name = name;
        Breed = breed;
        Age = age;
        Vaccine = vaccine;
    }
   public string AddVaccine (string vaccine)
   {
         return vaccine;
    }
}

Object:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//set size of the array for dogs and vaccines
Dog[] dogs = new Dog[20];
string [] vaccine = new string[10];

bool repeat = true;
while(repeat)
{
    //show all menu
    Console.WriteLine("1. Add a new dog\n2. Remove a dog\n3. Add Vaccine\n4. List all dogs\n5. Exit\n");
    Console.Write("Selection: ");
    int selection = int.Parse(Console.ReadLine());

    switch(selection)
    {
        case 1:

            //ask the user for the info about the car so we can add it to the object
            Console.Write("Enter dog name: ");
            string name = Console.ReadLine();
            Console.Write("Enter dog breed: ");
            string breed = Console.ReadLine();
            Console.Write("Enter dog age: ");
            int age = int.Parse(Console.ReadLine());

            //finding an available location in the list
            for(int i = 0; i < dogs.Length; i++)
            {
                if(dogs[i] == null)
                {
                    dogs[i] = new Dog(name, breed, age, string.Empty);
                    Console.WriteLine("Dog successfully added");
                    break;
                }      
            }  
            break;
            case 2:
                Console.Write("Enter the dog name:");
                string dogName = Console.ReadLine();
                bool removed = false;
                for(int i = 0; i< dogs.Length; i++)
                {
                    Dog dog = dogs[i];
                    if(dog!=null && dog.Name == dogName)
                    {
                        
                        dog = null;
                        dogs[i] = dog;
                        removed = true;
                        Console.WriteLine("{0} was successful removed", dogName);
                        break;
                    }
                    if (!removed)
                    {
                        Console.WriteLine("{0} was not found", dogName);
                    }
                }
            break;
            case 3:
                Console.Write("Enter the dog name:");
                string name2 = Console.ReadLine();
                bool added = false;
                for(int i = 0; i< dogs.Length; i++)
                {
                    Dog dog = dogs[i];
                    if(dog!=null && dog.Name == name2)
                    {
                        Console.Write("Enter the vaccine name:");
                        string vac = Console.ReadLine();
                        if(vac == null)
                        {
                            Console.WriteLine("Vaccine not added");
                        }
                        else
                        {
                            dog.Vaccine = vac;
                            added = true;
                            Console.WriteLine("{0} was successful added", vac);
                            break;
                        }
                        break;
                    }
                    if (!added)
                    {
                        Console.WriteLine("{0} was not found", name2);
                    }
                }
                break;
            case 4:
                bool listIsEmpty = true;
                foreach(Dog dog in dogs)
                {
                    if(dog != null)
                    {
                        ShowDogInfo(dog);
                        listIsEmpty = false;
                    }
                }
                if(listIsEmpty)
                {
                    Console.WriteLine("No dogs in the list");
                }
            break;
            case 5:
                Console.WriteLine("Thank you for using Rochester Doggie System");
                repeat = false;
                break;
            default:
                Console.WriteLine("Invalid selection");
                break;
        }
    }

void ShowDogInfo(Dog dog)
{
    Console.WriteLine("Name: {0}", dog.Name);
    Console.WriteLine("Breed: {0}", dog.Breed);
    Console.WriteLine("Age: {0}", dog.Age);
    Console.WriteLine("Vaccine: {0}", dog.Vaccine);
    Console.WriteLine();
}

I am expecting that the result will show the Console.WriteLine() if the input is successfully added or removed. I also am expecting to show all list of dogs if the user select 4 otherwise they can select 5 (i have tested and it works in which it exits from the program).

I have re-examined all lines so I do not see any wrong with it unless I have someone who might spot that I have missed something.

>Solution :

Name = value;: in the setter, you call the setter, which calls the setter etc.
This results in the observed stack overflow.

Change your properties to auto-properties:

public class Dog
{
    public string Name { get; set; }
// etc.
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading