【问题标题】:c# call object by unique propertyc#通过唯一属性调用对象
【发布时间】:2010-11-13 05:40:28
【问题描述】:

我有一个类推荐。当您在类中创建对象时,它会检查输入字符串是否唯一(因此绝不允许重复对象)。但是当我发现输入字符串 str1 等于先前创建的对象的字符串时,我不想创建新对象或只是返回 false,而是想更改已创建对象的属性。但我无法弄清楚如何做到这一点,因为该方法无法知道对象的名称。但我知道它的独特之处!我觉得这必须足以以某种方式调用它,然后做我需要做的事情。

有什么想法吗?
谢谢!

这是课程:

public class Referral
{
    public class Referral
    {
        public string URL;
        public Dictionary<string, int> Keywords = new Dictionary<string, int>();

        private static Dictionary<string, string> URLs = new Dictionary<string, string>();
        private int HowManyURLs;
        private bool UniqueURL;
        private bool UniqueKeyword;

        public Referral(string MyURL, string MyKeyword, int MyOccurrences)   //Constructor
        {
    if (HowManyURLs == 0)
    {
        URL = MyURL;
        Keywords.Add(MyKeyword, MyOccurrences);
        URLs.Add(MyURL, MyKeyword);
        HowManyURLs++;
    }

    else
    {
        // RESET FLAGS
        UniqueURL = true;
        UniqueKeyword = true;

        for ( int i = 0; i < HowManyURLs; i++ )
        {
        if ( URLs.ContainsKey( MyURL ) )
        {
            // TRIP URL FLAG
            UniqueURL = false;

            // NOW CHECK KEYWORDS OF URL << THIS IS WHAT I CAN'T DO!
            if ( URLs.ContainsKey( MyKeyword ) )
            {
            // TRIP KEYWORD FLAG
            UniqueKeyword = false;

             // ADD TO OCCURRENCES
    //      Referral[MyURL].Occurrences += MyOccurrences;
            }
        }
        }

    // IF BOTH FLAGS TRUE
    if  ( UniqueURL == true && UniqueKeyword == true )
    {
        URL = MyURL;
        Keywords.Add(MyKeyword, MyOccurrences);
        URLs.Add(MyURL, MyKeyword);
        HowManyURLs++;
    }

    }

        }
    }

【问题讨论】:

  • 当涉及到你的问题和你的代码时,我对你想要做什么感到有点困惑。一方面,这不是一种方法,它是一个构造函数,因此返回 false 不是一种选择,不创建新的 Referral 也不是一种选择。此外,您的 for 循环似乎每次都通过循环执行相同的检查,而没有任何更改。你的第二个 if 应该是:if (Keywords.ContainsKey(MyKeyword)) 吗?

标签: c# object methods unique call


【解决方案1】:

您为什么不在推荐对象本身之外创建一个推荐对象列表?这样,您可以检查列表以查看符合您条件的对象是否已经存在。如果是这样,请更新该对象。如果没有,请创建一个新的并将其添加到列表中。

要么使用静态列表,要么使用静态列表,也在引用类之外声明。

我不能 100% 确定你的目标是什么,所以希望其中一种方法对你有用。

【讨论】:

  • 为什么在推荐类之外?如果它的唯一使用位置是在 Referral 构造函数中,则没有理由将其暴露在类之外。
  • 在我看来,他们正试图维护一组对象,并希望在不存在时添加新对象并在存在时更新现有对象。这些对象的集合似乎是完成此任务的最佳方式。
【解决方案2】:

尝试使用Dictionary.TryGetValue 方法?

我不太明白你想用你的代码做什么,现在有点晚了。

【讨论】:

    【解决方案3】:

    您需要创建一个控制器类来维护引用对象的集合,以实现对象的先决条件检查和更新。

    类似:

    public class Referrals
    {
        private List<Referral> refs;
    
        public class Referrals()
        {
            this.refs = new List<Referral>();
        }
    
        public Referral Add(string MyURL, string MyKeyword)
        {
           var ref = this.refs.Find(delegate(Referral r) { return r.URL == MyURL; });
           if (ref != null)
           {
               if (ref.Keyword == MyKeyword)
               {
                   ref.IncrementOccurrences();
               }
           }
           else
           {
               ref = new Referral(MyURL, MyKeyword);
               this.refs.Add(ref);
           }
           return ref;
        }
    }
    

    我会将您的推荐类更改为不将 Occurrences 作为变量。这应该由对象私下处理:

    public class Referral
    {
       private string url;
       private string keyword;
       private int occurrences;
    
       public Referral(string MyURL, string MyKeyword)
       {
          this.url = MyURL;
          this.keyword = MyKeyword;
          this.occurrences = 1;
       }
    
       public int Occurrences { get { return this.occurrences; } }
       public string URL { get { return this.url; } }
       public string Keyword { get { return this.keyword; } }
    
       public void IncrementOccurrencies()
       {
          this.occurrences++;
       } 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 2019-10-11
      相关资源
      最近更新 更多