我有一个类推荐。当您在类中创建对象时,它会检查输入字符串是否唯一(因此绝不允许重复对象)。但是当我发现输入字符串 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++;
}
}
}
}