0

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;
                }
            }

        }
 }
4

1 に答える 1

3

この宣言が問題です:

public IEnumerable<RuleViolation> GetRuleViolations()

このメソッドはクラスには存在しません。どのクラスだと思った?どのクラスに配置したい場合でも、そこに移動します。

いくつかの注意点:

  • コードをインデントするように Visual Studio に依頼すると、何が起こっているのかをよりよく理解できます。たとえば、特に理由もなくRuleViolationさらにインデントしました。DinnerViolation
  • 実際に正当な理由がない限り、ネストされたクラスを使用しません。トップレベルのクラスは推論が容易です。
  • ソース ファイルごとに 1 つのクラスを保持すると、スコープと何がどこにあるかについて混乱しにくくなります。
于 2013-06-05T17:14:30.617 に答える