I have a class Referrals. When you create an object in the class, it checks that the input strings are unique (and therefore never allows duplicate objects). But when I find that input string str1 is equal to that of a previously created object, instead of creating a new object or just returning false, i want to change a property of the already created object. But i can't figure out how to do this, as the method has no way of knowing the name of the object. But I know something unique about it! I feel like this must be enough to somehow call it, and do what I need to do.
我有一个班级推荐。在类中创建对象时,它会检查输入字符串是否唯一(因此永远不允许重复的对象)。但是当我发现输入字符串str1等于先前创建的对象的输入字符串时,我想要更改已创建对象的属性,而不是创建新对象或只返回false。但我无法弄清楚如何做到这一点,因为该方法无法知道对象的名称。但我知道它的独特之处!我觉得这必须足以以某种方式称呼它,做我需要做的事情。
Any ideas?
THANKS!
有任何想法吗?谢谢!
Here is the class:
这是班级:
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++;
}
}
}
}
3 个解决方案
#1
Why don't you create a List of Referral objects outside of the Referral object itself? That way you can check the List to see if the object with your criteria already exists. If so, update that object. If not, create a new one and add it to the list.
为什么不在Referral对象本身之外创建一个Referral对象列表?这样,您可以检查列表以查看具有您的条件的对象是否已存在。如果是,请更新该对象。如果没有,请创建一个新的并将其添加到列表中。
Either that or use a static list, also declared outside of the Referrals class.
或者使用静态列表,也在Referrals类之外声明。
I'm not 100% sure where you're going with this, so hopefully one of those approaches will work for you.
我不是百分百肯定你要去哪里,所以希望其中一种方法适合你。
#2
Try using the Dictionary.TryGetValue method?
尝试使用Dictionary.TryGetValue方法?
I didn't quite understand what you're attempting to do with your code, and it's a bit late.
我不太明白你试图用你的代码做什么,而且有点晚了。
#3
You need to create a controller class that will maintain a collection of referral objects in order to achieve the prerequisite checking and updating of the object.
您需要创建一个控制器类,该类将维护一个引用对象的集合,以实现对象的先决条件检查和更新。
Something like:
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;
}
}
And I would change your Referral class to not take in Occurrences as a variable. This should be handled privately to the object:
我会将您的推荐类更改为不将事件作为变量。这应该私下处理对象:
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++;
}
}
#1
Why don't you create a List of Referral objects outside of the Referral object itself? That way you can check the List to see if the object with your criteria already exists. If so, update that object. If not, create a new one and add it to the list.
为什么不在Referral对象本身之外创建一个Referral对象列表?这样,您可以检查列表以查看具有您的条件的对象是否已存在。如果是,请更新该对象。如果没有,请创建一个新的并将其添加到列表中。
Either that or use a static list, also declared outside of the Referrals class.
或者使用静态列表,也在Referrals类之外声明。
I'm not 100% sure where you're going with this, so hopefully one of those approaches will work for you.
我不是百分百肯定你要去哪里,所以希望其中一种方法适合你。
#2
Try using the Dictionary.TryGetValue method?
尝试使用Dictionary.TryGetValue方法?
I didn't quite understand what you're attempting to do with your code, and it's a bit late.
我不太明白你试图用你的代码做什么,而且有点晚了。
#3
You need to create a controller class that will maintain a collection of referral objects in order to achieve the prerequisite checking and updating of the object.
您需要创建一个控制器类,该类将维护一个引用对象的集合,以实现对象的先决条件检查和更新。
Something like:
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;
}
}
And I would change your Referral class to not take in Occurrences as a variable. This should be handled privately to the object:
我会将您的推荐类更改为不将事件作为变量。这应该私下处理对象:
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++;
}
}