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";
}
}
}
}
}