2009-04-20 47 views
0

下面是我创建的类,用于跟踪我荣耀的数据录入和检索应用中的当前Person。一旦他们选择一个人,它会调用constrtuctor,然后调用数据库来填充所有其他信息。而且,在整个计划中,他们将能够改变各个领域。正确使用对象属性

考虑到这一点,我有下面的设置正确吗?我对属性和使用对象存储跨多种形式的数据exprrenced,并会感谢任何见解。

谢谢!

class CurrentPerson 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string MiddleName { get; set; } 
    public string SuffixID { get; set; } 
    public string TitleID { get; set; } 
    public string SocialSn { get; set; } 
    public string BirthDate { get; set; } 
    public string Gender { get; set; } 
    public string DlNumber { get; set; } 
    public string DlStateID { get; set; } 
    public string PrimaryRace { get; set; } 
    public string SecondaryRace { get; set; } 
    public string EmailAddress { get; set; } 
    public string MaritalStatus { get; set; } 
    public string InsertProgram { get; set; } 
    public string InsertUserID { get; set; } 
    public string UpdateProgram { get; set; } 
    public string UpdateUserID { get; set; } 
    public string LockID { get; set; } 

    public int PersonID { get; set; } 
    public int ClientID { get; set; } 
    public int ResidencyCountyID { get; set; } 
    public int ResponsibilityCountyID { get; set; } 

    public bool HispanicOriginFlag { get; set; } 
    public bool CitizenFlag { get; set; } 
    public bool VeteranFlag { get; set; } 

    public DateTime DeathDate { get; set; } 
    public DateTime InsertDateTime { get; set; } 
    public DateTime UpdateDateTime { get; set; } 

    // Put the default Constructor back in 
    public CurrentPerson(){} 

    // Custom Constructor that needs the PersonID 
    public CurrentPerson(int pID) 
    { 
     PersonID = pID; 

     // Methods to get rest of data here 
    } 
} 

回答

3

是的,看起来不错。 你可以,顺便说一句,设定的get /接入设置以及,使其读/写唯一公开

public DateTime DeathDate 
{ 
    get; 
    private set; 
} 
+0

如果我有私人设置,我仍然可以从我的表单设置变量? – 2009-04-20 15:55:27

+1

你仍然可以在你的表单上使用这个变量,你不能指定它 - 你的代码将不能编译。 – 2009-04-20 15:56:41

+0

通过一个构造函数,将是唯一的方法来设置它是否是私有的?谢谢! – 2009-04-20 15:58:06

1

这在技术上是罚款。他们都很好地声明。

但是,通常,对于数据库应用程序,您不想使用自动属性,因为属性设置器通常是进行某些验证的好地方,并且可能会将属性/对象标记为“脏”并且需要保存某种程度上。

1

自动属性总是被获取和设置,所以你无法控制关于属性设置(将实例标记为脏或其他)。因此,虽然这仅仅是一个可接受的类,但我通常会发现自动属性很少适用。