Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

C#
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
Posted
Updated 12-Feb-14 4:49am
v8
Comments
ZurdoDev 12-Feb-14 8:44am    
I don't follow what you are saying but you may want to look into using the Reflection classes.
Member 10408451 12-Feb-14 8:58am    
updated my question
Alexander Dymshyts 12-Feb-14 9:03am    
What are you trying to do? Why do you have two cicles?
Member 10408451 12-Feb-14 9:07am    
I have log file data with msgIDs and buscolors
I need to find how the message with same msgId is passing to different buses using color
like Red,Yellow,Green , Orange
Now I have list class which holds data of msgIDs and buscolors
How to seperate different colors of same msgIds
Alexander Dymshyts 12-Feb-14 9:15am    
So you have list of values of type class, where class consists of two properties - id and color. And you need to make from one list smaller lists of different colors? Right?

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.
 
Share this answer
 
Comments
Member 10408451 12-Feb-14 9:31am    
I have new to linq

I modified my question with my code
please help me
Alexander Dymshyts 12-Feb-14 9:39am    
type in Google sort list by value linq and you will see examples of sorting in Linq, and to make smaller list you should look for a linq command - where.
johannesnestler 12-Feb-14 9:41am    
why sort?
Alexander Dymshyts 12-Feb-14 10:01am    
I think it will be easear for him to debug, if he will have Problems. In General you are right, and he can take values using where whithout sorting.
Have a look at this code:

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

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


C#
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?
 
Share this answer
 
v2
Comments
Member 10408451 12-Feb-14 9:48am    
Hi,
this helps me a lot
but I need to list the colors of same msgId
not for same color

Thanks
johannesnestler 12-Feb-14 11:08am    
so just replace the search criteria inside Where: Where(info => info.MessageId = 3); I love LINQ (and I hate CAN -I guess automotive industry?)
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:
C#
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:
C#
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.
 
Share this answer
 
Comments
Member 10408451 12-Feb-14 10:49am    
Hi,

I tried as you said and updated my original question with a comment
Please check it??
BillWoodruff 12-Feb-14 11:50am    
Well, I am afraid that's not going to work well for you. You've made the 'CanBusColorTrackerclass dynamic rather than static, and you've created a List<CanBusColorTracker> which is not the idea.

I think the key idea I expressed is that you have a single instance of a tracking class, separate from your main Class, and you have one method call in your main Class to keep track of the MsgId's and which Colors they use.

One advantage of having a single-point in the main Class where the static Class is accessed is that you can surround that single-point with a #if/#endif preprocessor statement, and turn the tracking on, or off, during testing.

I think you can adapt the idea shown here, if you wish, by making the Dictionary static and putting it inside the main Class.

good luck, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900