簡體   English   中英

c#通過唯一屬性調用對象

[英]c# call object by unique property

我有一個課程推薦。 在類中創建對象時,它將檢查輸入字符串是否唯一(因此,永遠不允許重復的對象)。 但是,當我發現輸入字符串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++;
    }

    }

        }
    }

為什么不在參照對象本身之外創建參照對象列表? 這樣,您可以檢查“列表”以查看具有您的條件的對象是否已經存在。 如果是這樣,請更新該對象。 如果沒有,請創建一個新的並將其添加到列表中。

要么使用靜態列表,要么在Referrals類之外聲明靜態列表。

我不是100%知道您要怎么做,所以希望其中一種方法對您有用。

嘗試使用Dictionary.TryGetValue方法?

我不太了解您要如何使用自己的代碼,但為時已晚。

您需要創建一個控制器類,該類將維護引用對象的集合,以實現對象的先決條件檢查和更新。

就像是:

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

而且我會更改您的Referral類,以不將出現次數作為變量。 這應該對對象私下處理:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM