简体   繁体   中英

Using set to change property value in a constructor, C#

The program I wrote works, and prints fine. It creates two objects fine. One object is to be created using the no-arg default constructor, and the other is to be created from the non-default constructor. The only difference is I am supposed to use the set keyword for Author to create a default value. So that when I go to create the object with the wrong author name it will change it using the set keyword.

When I enter in the wrong value for Book1.Author for the non-default constructor, it changes both Author names in both objects. How do I only allow it to change the author name in my Book1 object?

using System;
namespace Book
{
    public class Book
    {
        private string _Author;
        private string _Title;
        private string _Keywords;
        private string _publicationDate;
        private string _ISBN;

        public Book()
        {
            Author = "";
        }

        public Book(string title, string author, string publicationDate, string keywords, string isbn)
        {
            Title = title;
            Author = author;
            Keywords = keywords;
            PublicationDate = publicationDate;
            ISBN = isbn;
        }
        public string Title { get => _Title; set => _Title = value; }
        public string Author { get => _Author; set => _Author = "Mary Delamater and Joel Murach, "; }
        public string Keywords { get => _Keywords; set => _Keywords = value; }
        public string PublicationDate { get => _publicationDate; set => _publicationDate = value; }
        public string ISBN { get => _ISBN; set => _ISBN = value; }

        public override string ToString()
        {
            return Title + Author + PublicationDate + "Keywords: " + Keywords + "ISBN " + ISBN;
    
        
        }

        
    

    }
}

using System;

namespace Book
{
    class Program
    {
        static void Main(string[] args)
        {
            Book Book1 = new Book("murach's ASP.NET Core MVC, ", "Mary Delamater and Joel Murach, ", "January 2020, ", "C#, Programming, MVC, ASP.NET, Core, Beginner", "978-1-943872-49-7");
            Console.WriteLine(Book1.ToString());
            Book Book2 = new Book();
            Book2.Title = "C# In Depth, ";
            Book2.Author = "John Skeet, ";
            Book2.PublicationDate = "March 23, 2019, ";
            Book2.Keywords = "C#, InDepth";
            Book2.ISBN = "9781617294532";
            Console.WriteLine(Book2.ToString());

        }
    }
}

Your understanding of the program flow is wrong. In your constructor when you call your properties to set value, the program flow will run the set part which you hardcoded to a string. Now if you try to save your value via this property by an object or in a constructor it will always set to the hardcoded value irrespective of what you want. To store a custom value, you have to use set=>value and to apply your business constraint, you can write logical conditions inside your property which is why properties are used for to achieve encapsulation.

Something like this:

public string Author{
  get{
    return _Author;
  }
  set{
    if()//your condition to validate if author is wrong
      _Author = ""; //your expected correct author name
    else
      _Author = value
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM