I'm trying to create a class with a built-in condition for one of its properties. Since I'm new to OOP I would like to ask which techniques should be made and how to implement them.
Assuming I have a class:
public class Item
{
private string itemName;
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
private bool isPerishable;
public bool IsPerishable
{
get { return isPerishable; }
set { isPerishable = value; }
}
private DateTime expiryDate;
public DateTime ExpiryDate
{
get { return expiryDate; }
set { expiryDate = value; }
}
}
I simply want to have built-in logic to the class (maybe methods or something) that if isPerishable is true then it asks for an expiry date. Could the logic be called outside the Main program (within the class itself)? Please post an example of how to achieve this.
private bool isPerishable;
public bool IsPerishable
{
get { return isPerishable; }
set
{
isPerishable = value;
if(value && expiryDate == default(DateTime))
{
Console.Write("Enter an expiry date: ");
expiryDate = Date.Parse(Console.ReadLine());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With