[英]How to make sure a class is valid before referencing?
public class Person
{
public string FullName { get; set; }
public int? PhoneNumber { get; set; }
public int? CarQTY { get; set; }
}
如果有人沒有完全填充對象,我想拋出異常。 例如,如果 fullName、phoneNumber 或 carQTY 為 null,我想在 get 方法上拋出異常。
我真的必須這樣做嗎?
public class Person
{
string _FullName;
int? _PhoneNumber;
int? _CarQTY;
private Boolean IsValid()
{
Boolean condition = true;
condition = (FullName != null) && (PhoneNumber != null) && (CarQTY != null);
return condition;
}
public string FullName
{
get
{
if (!IsValid()) throw new System.ArgumentException("Parameter cannot be null", "fullName is null");
return _FullName;
}
set
{
_FullName = value;
}
}
public int? PhoneNumber
{
get
{
if (!IsValid()) throw new System.ArgumentException("Parameter cannot be null", "phoneNumber is null");
return _PhoneNumber;
}
set
{
_PhoneNumber = value;
}
}
public int? CarQTY
{
get
{
if (!IsValid()) throw new System.ArgumentException("Parameter cannot be null", "carQTY is null");
return _CarQTY;
}
set
{
_CarQTY = value;
}
}
}
正如評論中所建議的,強制通過構造函數創建對象是最有效的方法:
public class Person
{
public string FullName { get; private set; }
public int PhoneNumber { get; private set; }
public int CarQTY { get; private set; }
public Person(string fullName, int phone, int carQty){
FullName = fullName;
PhoneNumber = phone;
CarQTY = carQty;
}
}
如果您希望在創建對象后允許對其進行修改,則 setter 也可能是公共的。
嗯,這可能是一個有爭議的問題,即應該在何處以及如何驗證對象。 如果一個對象沒有按照它應該的方式創建,它是否應該抱怨? 還是應該由創建者負責檢查即將創建的對象的完整性?
我可以消化讓我說第二個應該。
你想創造我,那就正確地創造我吧!
因此,我想提出以下建議:
public class Person
{
public string FullName { get; set; }
//I suggest the PhoneNumber should be a string type.
public string PhoneNumber { get; set; }
public int CarQTY { get; set; }
public Person() { }
public Person(string fullName, string phoneNumber, int carQTY) : this()
{
FullName = fullName;
PhoneNumber = phoneNumber;
CarQTY = carQTY;
}
public static Person Parse(string fullName, string phoneNumber, int carQTy)
{
if (!TryParse(fullName, phoneNumber, carQTy, out Person person))
throw new ArgumentException("Incorrect Arguments");
return person;
}
public static bool TryParse(string fullName, string phoneNumber, int carQTy, out Person person)
{
person = null;
if (!string.IsNullOrEmpty(fullName) &&
!string.IsNullOrEmpty(phoneNumber) &&
carQTy > 0)
person = new Person(fullName, phoneNumber, carQTy);
return person == null ? false : true;
}
public override string ToString() =>
return $"{FullName}, {PhoneNumber}, {CarQTY}";
}
現在,您有兩個新的靜態函數( Parse
、 TryParse
),調用者可以使用它們來創建有效的對象。 所以你可以這樣做:
public class SomeClass
{
private void CreatePerson()
{
if(!Person.TryParse(fullName, phoneNumber, carQty, out Person person))
{
MessageBox.Show("Invalide Args");
return;
}
//You have a valid person object...
}
}
或者
public class SomeClass
{
private void CreatePerson()
{
Person person;
try
{
person = Person.Parse(fullName, phoneNumber, carQty);
//You have a valid person object...
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.