对部分类进行验证

时间:2012-03-04 23:15:06

标签: c# wpf wcf validation

我有这个代码用于验证从WCF服务创建的Customer类:

public partial class Customer : IDataErrorInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }

#region IDataErrorInfo Members

public string Error
{
    get { throw new NotImplementedException(); }
}

public string this[string columnName]
{
    get
    {
        string result = null;
        if (columnName == "FirstName")
        {
            if (string.IsNullOrEmpty(FirstName))
                result = "Please enter a First Name";
        }
        if (columnName == "LastName")
        {
            if (string.IsNullOrEmpty(LastName))
                result = "Please enter a Last Name";
        }
       if (columnName == "Age")
        {
            if (Age < = 0 || Age >= 99)
                result = "Please enter a valid age";
        }
        return result;
    }
}

#endregion

}

我在this [string columnName]方法的定义中得到了错误,可能是因为它是一个部分类:

Member names cannot be the same as their enclosing type

你知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

所有这些都告诉您,您的会员名称不能与班级名称相同。例如,您不能在Customer对象上拥有Customer方法或属性。

例如提防

public class Customer 
{
   public void Customer()  //<- You cannot do this, remove the void Customer should only be the constructor
   { } 
}

所以我猜这个问题是在其他地方,而不是你发布的代码片段。