-1

I made a class called Book, and it has a variable called Author. I ask the user to input the name of the author, and if they input something that isn't "Adam" or "Adam Omar", Author becomes "Unknown". When I run the program, if I enter either "Adam, "Adam Omar", or a different string, the program outputs this infinitely: at Testing.Book.set_Author(System.String), and this line: if (value == "Adam Omar" || value == "Adam") in the Book Class shows this error: System.StackOverflowException: 'Exception_WasThrown'

(This is in a test Project, that is why there is no 'failsafe' for if the user enters numbers, and why Book only has a single variable).

Main Class:

namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            string userInput;
            Console.Write("What is the name of the author? ");
            userInput = Console.ReadLine();
            Book myFirstBook = new Book(userInput);

            Console.WriteLine(myFirstBook.Author);

            Console.ReadLine();

        }
    }
}

Book Class:

namespace Testing
{
    internal class Book
    {
        private string author;

        public Book(string inputAuthor)
        {
            Author = inputAuthor;
        }

        public string Author
        {
            get
            {
                return Author;
            }
            set
            {
                if (value == "Adam Omar"  || value == "Adam")
                {
                    Author = value;
                }
                else
                {
                    Author = "Unknown";
                }
            }
        }
    }
}
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
Adam Omar
  • 3
  • 3
  • I couldn't get it to work, the program still goes wrong at the if statement. Could it be due to "value" not working properly? – Adam Omar Jul 28 '23 at 18:00
  • 2
    The problem is that you're trying to assign the value of Author to itself, instead of a backing field. When you do this it calls the setter again, which calls the setter...This causes infinite recursion, resulting in a stack overflow. – Michael Roy Jul 28 '23 at 18:06
  • Do you mean to set the private field author to value? – SoftwareDveloper Jul 28 '23 at 18:14

2 Answers2

1

You have a field called "author" and a property called "Author".

For convention and clarity sake, rename your field to "_author".

Now in your property, you are incorrectly returning the property itself. Change your getter to:

    get
    {
        return _author;
    }

Same with your property setter, you need to set the field, not the property. Change your setter to:

    set
    {
        if (value == "Adam Omar"  || value == "Adam")
        {
            _author = value;
        }
        else
        {
            _author = "Unknown";
        }
    }

To understand more about fields and properties refer: What is the difference between a field and a property?

TP95
  • 175
  • 8
0

You're doing Properties wrong. Your implementation has created a circular reference that is causing your error. Your code...

Author = value;

...is repeatedly calling the Author property to set it, over and over and over and over until you hit your stack overflow exception. If you run your application and step through it you will see that your Author property is repeatedly calling itself.

You need another variable to back the property....

Try this...

public Book(string inputAuthor)
{
    Author = inputAuthor;
}

private string author;

public string Author
{
    get
    {
        return author;
    }
    set
    {
        if (value == "Adam Omar"  || value == "Adam")
        {
            author= value;
        }
        else
        {
            author= "Unknown";
        }
    }
}


quaabaam
  • 1,808
  • 1
  • 7
  • 15