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

本文关键字:结构 我的 代码 枚举 委托 期望 接口 | 更新日期: 2023-09-27 18:16:59

我正在学习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;
                }
            }
        }
 }

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

这个声明就是问题所在:

public IEnumerable<RuleViolation> GetRuleViolations()

这个方法在类中不存在。你认为它在哪个班?无论你想把它放在哪个类中,把它移到那里。

注意事项:

  • 如果你让Visual Studio缩进你的代码,你会得到一个更好的想法发生了什么。例如,您没有特别的原因将RuleViolation缩进到DinnerViolation之外。
  • 我不会使用嵌套类,除非你有一个很好的理由。顶层类更容易理解。
  • 如果你为每个源文件保留一个类,就很难对作用域和在哪里感到困惑。