我的代码中的预期类,委托,枚举,接口或结构

时间:2013-06-05 17:10:55

标签: c# asp.net inner-classes

我正在研究NerdDinner教程。我的代码中出现错误。我觉得我搞砸了内心阶级和布拉克的数量。但我真的不知道如何解决这个问题。有人可以帮帮我吗?谢谢。以下是我的代码:

namespace NerdDinner.Models
{
    public partial class DinnerViolation
    {
        public bool isValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            yield break;
        }

        public void OnValidate(ChangeAction action)
        {
            if (!isValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }

        public class RuleViolation
        {
            public string ErrorMessage { get; private set; }
            public string PropertyName { get; private set; }

            public RuleViolation(string errormessage, string propertyName)
            {
                ErrorMessage = errormessage;
                PropertyName = propertyName;
            }
        }

            public IEnumerable<RuleViolation> GetRuleViolations()//Error appears here
            {

                if (String.IsNullOrEmpty(Title))
                    yield return new RuleViolation("Title required", "Title");

                if (String.IsNullOrEmpty(Description))
                    yield return new RuleViolation("Description required", "Description");

                if (String.IsNullOrEmpty(HostedBy))
                    yield return new RuleViolation("HostedBy", "HostedBy");

                if (String.IsNullOrEmpty(Address))
                    yield return new RuleViolation("Address required", "Address");

                if (String.IsNullOrEmpty(Country))
                    yield return new RuleViolation("Country required", "Country");

                if (String.IsNullOrEmpty(ContactPhone))
                    yield return new RuleViolation("ContactPhone required", "ContactPhone");

                if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
                    yield return new RuleViolation("Phone# does not match country", "ContactPhone");

                yield break;
            }

        public class PhoneValidator
        {
            static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>() {           
            { "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},            
            { "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},            
            { "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},    
            };

            public static bool IsValidNumber(string phoneNumber, string country)
            {
                if (country != null && countryRegex.ContainsKey(country))
                    return countryRegex[country].IsMatch(phoneNumber);
                else
                    return false;
            }

            public static IEnumerable<string> Countries
            {
                get
                {
                    return countryRegex.Keys;
                }
            }

        }
 }

1 个答案:

答案 0 :(得分:3)

这个声明就是问题:

public IEnumerable<RuleViolation> GetRuleViolations()

此方法在类中不存在。你认为认为是哪一堂课?无论你想要哪一堂课,都要把它移到那里。

需要注意几点:

  • 如果您要求Visual Studio缩进代码,您将更好地了解正在发生的事情。例如,由于没有特殊原因,您比RuleViolation缩进了DinnerViolation
  • 我不会使用嵌套类,除非你真的有充分的理由。顶级课程更容易推理。
  • 如果你为每个源文件保留一个类,那么很难对范围和内容感到困惑。