如何在构造函数类中查找数据 [英] How can I find the data in constructor class

查看:83
本文介绍了如何在构造函数类中查找数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下代码存储MsgId的不同总线颜色



< pre lang =c#> public class CanBusColorTracker
{
public static 字典<字符串,列表< BusesColor>> dctMsgIDColors = new Dictionary< string,List< BusesColor>>();

public void 跟踪(BusesColor buscolor,字符串 msgId)
{
// 必须避免重复的密钥
if (!dctMsgIDColors.Keys.Contains(msgId))
{
dctMsgIDColors.Add(msgId, new List< BusesColor>());
}

// 我们实际上不必避免重复
// 颜色,但是,为什么不呢。
if (!dctMsgIDColors [msgId] .Contains(buscolor))
{
dctMsgIDColors [msgId] .Add(buscolor);
}
}
}
public class CANBusColorMsgIdMap
{
string msgId;
public BusesColor BusColor { get ; set ; }
public 列表< CanBusColorTracker> MsgIdListColors { get ; set ; }

public string MsgId
{
< span class =code-keyword> 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;
}
}







但我没有收到MsgIdListColors的任何数据

它显示为空对象

我不明白。为什么?







谢谢

John

解决方案

所以我会按照以下方式进行:

1.创建字典,其中key是颜色的名称,值将列出id。

2.使用linq对大列表进行排序,使蓝色颜色接近蓝色,绿色接近绿色等等。

3.然后,也使用linq,来自您的大列表较小的列表,其中只有一种颜色(蓝色等)和所有具有此颜色的ID。

4.创建int类型列表并从创建的列表中添加所有ID。 br />
5.添加到词典颜色并在4个列表中创建。

6.转到3.

希望这将有所帮助,如果没有,显示你修改过的代码,我们会试着看错了。


看看这段代码:



我刚刚模拟了我猜你已经创建了类(和一个BusColor枚举)



  enum  BusColor {红色,黄色,绿色,橙色} 
class CANInfo
{
public int MessageId { get ; set ; }
public BusColor Color { get ; set ; }
}





列表< CANInfo> listCANInfosAllTogether =  new 列表< CANInfo> 
{
new CANInfo {MessageId = 1 ,Color = BusColor.Green} ,
new CANInfo {MessageId = 2 ,Color = BusColor.Green},
new CANInfo {MessageId = 3 ,Color = BusColor.Green},
new CANInfo {MessageId = 1 ,Color = BusColor.Orange},
CANInfo {MessageId = 2 ,Color = BusColor.Orange},
new CANInfo {MessageId = 3 ,Color = BusColor.Orange},
new CANInfo {MessageId = 1 ,Color = BusColor.Red},
new CANInfo {MessageId = 2 ,Color = BusColor.Red},
new CANInfo {MessageId = 3 ,Color = BusColor.Red}
};

列表< CANInfo> listOnlyRed = listCANInfosAllTogether.Where(info = > info.Color == BusColor.Red)。ToList();





这就是你需要的吗?


有很多方法你可以拿一个List< CANBusColorMsgIdMap>并通过它,并为其中的每个唯一的MsgId构建一个List< Color> ;.



但是,我认为这是错误的策略:我建议你创建用于保存msgId和List< Color>的数据结构。一次在静态类中,然后,在你的CANBusColorMsgIdMap类的构造函数中,做正确的事来更新这些数据结构。



结果会很多更简单:您只需将现有数据存储在跟踪类中,并将该信息写入您的日志文件。



这是一个非常粗略的草图在您上次发布的类定义中:静态类包含Dictionary< string,List< Color>>以及一种更新该Dictionary的方法:

  public   static   class  CanBusColorTracker 
{
public static 字典<字符串,列表<颜色>> dctMsgIDColors = new Dictionary< string,List< Color>>();

public static void 跟踪(颜色buscolor,字符串 msgId)
{
// 必须避免重复密钥
如果(!dctMsgIDColors.Keys.Contains(msgId))
{
dctMsgIDColors.Add(msgId, new List< Color>());
}

// 我们实际上不必避免重复
// 颜色,但是,为什么不呢。
if (!dctMsgIDColors [msgId] .Contains(buscolor))
{
dctMsgIDColors [msgId] .Add(buscolor);
}
}
}

public class CANBusColorMsgIdMap
{
public string msgId;
public Color BusColor { get ; set ; }

public string MsgId
{
< span class =code-keyword> get
{
if (!String.IsNullOrEmpty(msgId))
return msgId.ToUpper();
else
return string .Empty;
}

set {msgId = value ; }
}

public CANBusColorMsgIdMap(Color buscolor, string msgId )
{
// 跟踪班级的新实例及其颜色
CanBusColorTracker.Track(buscolor,msgId);

this .BusColor = buscolor;
this .msgId = msgId;
}
}

假设您在正确位置的静态类中获得了对Dictionary的有效引用,您可以使用以下代码轻松枚举它:

  foreach  var  kvp  in  CanBusColorTracker.dctMsgIDColors)
{
Console.Write(kvp.Key + );
foreach var clr in kvp.Value)
{
Console.Write(clr + );
}
Console.WriteLine();
}

该代码将在VS'输出窗口中生成如下输出:



CFOO400颜色[红色]颜色[黄色]颜色[绿色]

18FFEEC颜色[黄色]

6CEEFEE颜色[红色]

C00000B颜色[绿色]颜色[黄色]


请注意,您可以使用动态类,而不是静态类;只要确保你创建一个实例,然后做正确的事情,这样你的类的构造函数就可以访问它。


Hi,

I am using following code for storing different bus colors of MsgId

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




But I am not getting any data into MsgIdListColors
It is showing as null object
I donot understand . why?



Thanks
John

解决方案

So I would do it following way :
1. Create dictionary, where key will be name of the colors, and values will list of ids.
2. Sort you big list using linq, so that the blue Colors will be near blue, green near green and so on.
3. Then, also using linq, get from your big list smaller list, where will be only one color(blue and so on) and all ids with this Color.
4. Create list of type int and add there all ids from created list.
5. Add to dictionary Color and created in 4 list.
6. Go to 3.
Hope this will help, if not, Show your modified code and we will try to see whats wrong.


Have a look at this code:

I just simulated the classes I guess you have created (and a BusColor enum)

enum BusColor {  Red, Yellow, Green, Orange }
class CANInfo
{
    public int MessageId { get; set; }
    public BusColor Color { get; set; }
}



List<CANInfo> listCANInfosAllTogether = new List<CANInfo>
          {
              new CANInfo { MessageId = 1, Color = BusColor.Green },
              new CANInfo { MessageId = 2, Color = BusColor.Green },
              new CANInfo { MessageId = 3, Color = BusColor.Green },
              new CANInfo { MessageId = 1, Color = BusColor.Orange },
              new CANInfo { MessageId = 2, Color = BusColor.Orange },
              new CANInfo { MessageId = 3, Color = BusColor.Orange },
              new CANInfo { MessageId = 1, Color = BusColor.Red },
              new CANInfo { MessageId = 2, Color = BusColor.Red },
              new CANInfo { MessageId = 3, Color = BusColor.Red }
         };

          List<CANInfo> listOnlyRed = listCANInfosAllTogether.Where(info => info.Color == BusColor.Red).ToList();



Is this what you need?


There are a number of ways you could take a List<CANBusColorMsgIdMap> and go through it, and construct, for each unique MsgId in it, a List<Color>.

But, I think that's the wrong strategy: I suggest you create the data structures to hold msgId and a List<Color> once in a static Class, and then, in the constructor of your CANBusColorMsgIdMap Class, "do the right thing" to update those data structures.

The outcome will be much simpler: you'll just take the existing data stored in the "tracking" Class, and write that information out to your log file.

Here's a very rough sketch based on your last posted Class definition: The static class contains a Dictionary<string,List<Color>>, and one method to update that Dictionary:

public static class CanBusColorTracker
{
    public static Dictionary<string, List<Color>> dctMsgIDColors = new Dictionary<string, List<Color>>();

    public static void Track(Color buscolor, string msgId)
    {
        // must avoid duplicate Keys
        if (! dctMsgIDColors.Keys.Contains(msgId))
        {
            dctMsgIDColors.Add(msgId, new List<Color>());
        }

        // 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
{
    public string msgId;
    public Color BusColor { get; set; }

    public string MsgId
    {
        get
        {
            if (!String.IsNullOrEmpty(msgId))
                return msgId.ToUpper();
            else
                return string.Empty;
        }

        set { msgId = value; }
    }

    public CANBusColorMsgIdMap(Color buscolor, string msgId)
    {
        // Track the new instance of the Class and its Color
        CanBusColorTracker.Track(buscolor, msgId);

        this.BusColor = buscolor;
        this.msgId = msgId;
    }
}

Assuming you get a valid reference to the Dictionary in the static Class in the right place, you can easily enumerate it with code like this:

foreach (var kvp in CanBusColorTracker.dctMsgIDColors)
{
    Console.Write(kvp.Key + " ");
    foreach (var clr in kvp.Value)
    {
        Console.Write(clr + " ");
    }
    Console.WriteLine();
}

That code would generate output like this in VS' Output window:

CFOO400 Color [Red] Color [Yellow] Color [Green]
18FFEEC Color [Yellow]
6CEEFEE Color [Red]
C00000B Color [Green] Color [Yellow]

Note that you can use a dynamic Class, rather than a static one; just make sure you create one instance of it, and do the right thing so your constructor of your Class can access it.


这篇关于如何在构造函数类中查找数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆