如何更正此词典代码 [英] How to correct this dictionary code
本文介绍了如何更正此词典代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将相同msgId的颜色添加到字典中
Hi,
I am trying add colors of same msgId into a dictionary using the following code
public class CanBusColorTracker
{
public static Dictionary<string, List<BusesColor>> dctMsgIDColors = new Dictionary<string, List<BusesColor>>();
public void Track(BusesColor buscolor, string msgId)
{
// must avoid duplicate Keys
if (!dctMsgIDColors.Keys.Contains(msgId))
{
dctMsgIDColors.Add(msgId, new List<BusesColor>());
}
// we don't really have to avoid putting duplicate
// Colors, but, why not.
if (!dctMsgIDColors[msgId].Contains(buscolor))
{
dctMsgIDColors[msgId].Add(buscolor);
}
}
}
public class CANBusColorMsgIdMap
{
string msgId;
public BusesColor BusColor { get; set; }
public List<CanBusColorTracker> MsgIdListColors { get; set; }
public string MsgId
{
get
{
if (!String.IsNullOrEmpty(msgId))
return msgId.ToUpper();
else
return string.Empty;
}
set { msgId = value; }
}
public CANBusColorMsgIdMap(BusesColor buscolor, string msgId)
{
this.MsgIdListColors = MsgIdListColors;
this.BusColor = buscolor;
this.msgId = msgId;
}
}
能否帮我解决这个问题
谢谢
John
Can you please help me in solving this
Thanks
John
推荐答案
1。以下声明什么都不做。删除它。
1. The following statement does nothing. Remove it.
this.MsgIdListColors = MsgIdListColors;
2.公共成员dctMsgIDColors被声明为静态,但您稍后会声明这些列表。如果是静态的,为什么列表?
3.就像Richard M.所说,你需要清楚地定义你想要的东西。我最好的猜测是......
2. The public member dctMsgIDColors is declared static but you later declare a list of these. If static, why the list?
3. Like Richard M. says, you need to clearly define what you want. My best guess is ...
public class CanBusColorTracker : Dictionary<string, List<BusesColor>> { }
代码中的其他地方...
elsewhere in your code ...
public static CanBusColorTracker Tracker = new CanBusColorTracker();
List<busescolor> colors;
string ID = "123";
// given string ID, lookup colors.
if ( Tracker.TryGetValue(ID, out colors) )
{
// do something with list of colors.
}</busescolor>
C#中的多地图通用集合类 - 可以存储重复键值对的字典集合类 [ ^ ]
问候,
Praveen nelge
Multi-map Generic Collection Class in C# - A Dictionary Collection Class that can Store Duplicate Key-value Pairs[^]
Regards,
Praveen nelge
这篇关于如何更正此词典代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文