0

クラス紹介があります。クラスにオブジェクトを作成すると、入力文字列が一意であるかどうかがチェックされます(したがって、オブジェクトの重複は許可されません)。しかし、入力文字列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++;
    }

    }

        }
    }
4

3 に答える 3

0

紹介オブジェクト自体の外部に紹介オブジェクトのリストを作成してみませんか?そうすれば、リストをチェックして、基準を持つオブジェクトがすでに存在するかどうかを確認できます。その場合は、そのオブジェクトを更新します。そうでない場合は、新しいものを作成してリストに追加します。

それか、またReferralsクラスの外部で宣言された静的リストを使用します。

私はあなたがこれでどこに行くのか100%確信が持てないので、うまくいけば、それらのアプローチの1つがあなたのために働くでしょう。

于 2009-07-23T01:31:37.447 に答える
0

Dictionary.TryGetValueメソッドを使用してみてください。

あなたがあなたのコードで何をしようとしているのかよくわかりませんでした、そしてそれは少し遅れています。

于 2009-07-23T01:46:14.510 に答える
0

オブジェクトの前提条件のチェックと更新を実現するには、参照オブジェクトのコレクションを維持するコントローラークラスを作成する必要があります。

何かのようなもの:

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

}
于 2009-07-23T13:05:42.637 に答える