Click here to Skip to main content
16,008,490 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# game/physics engine Pin
petter201219-Nov-13 6:26
petter201219-Nov-13 6:26 
GeneralRe: C# game/physics engine Pin
Mycroft Holmes19-Nov-13 13:29
professionalMycroft Holmes19-Nov-13 13:29 
GeneralRe: C# game/physics engine Pin
petter201219-Nov-13 21:58
petter201219-Nov-13 21:58 
QuestionCondition checking on like button of face book on page load Pin
Mohd Ishaque18-Nov-13 2:55
Mohd Ishaque18-Nov-13 2:55 
AnswerRe: Condition checking on like button of face book on page load Pin
Dave Kreskowiak18-Nov-13 4:16
mveDave Kreskowiak18-Nov-13 4:16 
Questionhow to add a information box in data grid view Pin
Izû Udayanga18-Nov-13 0:18
Izû Udayanga18-Nov-13 0:18 
AnswerRe: how to add a information box in data grid view Pin
Richard MacCutchan18-Nov-13 0:56
mveRichard MacCutchan18-Nov-13 0:56 
QuestionHow to save data to objects of common classes Pin
Member 1040845117-Nov-13 22:55
Member 1040845117-Nov-13 22:55 
Hi,

I am new to c#. I am reading the data from log file

And the outpur is as shown in the image.

Now I want to separate the messages of RED,YELLOW and GREEN message ID's into objects of common classes.

I am trying something like this in the following code.

C#
using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using ConsoleApplication13;

namespace ConsoleApplication13
{
    #region Buses

    public enum Buses
    {
        CANBusRed = 1,
        CANBusYellow = 2,
        CANBusGreen = 3,
        CANBusOrange = 4
    };

    #endregion


     #region Member Variables

        /// <summary>
        /// Objects of common classes
        /// </summary>
        public static CANBusDetails CANBusRedDetails = null; 
        public static CANBusDetails CANBusYellowDetails = null;
        public static CANBusDetails CANBusGreenDetails = null;
        public static CANBusDetails CANBusOrangeDetails = null;


        #endregion

    public class CANBusDetails

        {

         public List<string> CANBusMsgIDList { get; set; }
        }



    public class CANBusMsgIdMap
    {
        string msgId;
        public Buses Bus { get; set; }
        //public string CANBus { get; set; }
        //public string CANMsgId { get; set; }

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

            set { msgId = value; }
        }

        public CANBusMsgIdMap(Buses bus, string msgId)
        {
            this.Bus = bus;
            this.msgId = msgId;

        }
        public override string ToString() // Equals,GetHash also there.here I am using ToString
        {
            return this.Bus + ", " + this.msgId;
        }
    }



    class Program
    {

        static void Main(string[] args)
        {

            string[] fileContents = null;
            List<CANBusMsgIdMap> CANMsgIdList = new List<CANBusMsgIdMap>();

            //String seclogPath1 = "C:\\Users\\Vivek\\Desktop\\log files\\1302_P3\\logg2.asc";
            String seclogPath1 = @"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\1302_P3\logg2.asc";




            fileContents = File.ReadAllLines(seclogPath1);
            for (int Index = 0; Index < fileContents.Length; Index++)
            {
                string CANMsgId = string.Empty;
                string[] spaceSeperator = new string[] { " " };
                string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);

                // If the number of words in a line is less than 3 then its not a valid entry.
                if (lineWords.Length < (2 + 1))
                    continue;

                // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                // then skip the entry and go to the next line of log file
                if (lineWords[2].EndsWith("x"))
                    CANMsgId = lineWords[2].TrimEnd('x');
                else
                    continue;

                // Check the format of the CAN Msg Id, whether Hex or not.
                if (Regex.IsMatch(CANMsgId, @"^[0-9A-Fa-f]+$"))
                {
                    Buses CANBus = (Buses)Enum.Parse(typeof(Buses), (lineWords[1]));

                    CANMsgIdList.Add(new CANBusMsgIdMap(CANBus, CANMsgId));



                }

            }
            //foreach (CANBusMsgIdMap CANBusMsgIdMap in CANMsgIdList)
            //{
            //    Console.WriteLine(CANBusMsgIdMap);


            //}

            //for (double i = 0; i <= 10000000000000; i++)
            //{
            //}
                 #region Copy Distinct CAN Message IDs

                // Copying Distinct CANMessageIds
                if(CANBusRedDetails != null)
                    CANBusRedDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusRed, CANMsgIdList);

                if (CANBusYellowDetails != null)
                    CANBusYellowDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusYellow, CANMsgIdList);

                if (CANBusGreenDetails != null)
                    CANBusGreenDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusGreen, CANMsgIdList);

                if (CANBusOrangeDetails != null)
                    CANBusOrangeDetails.CANBusMsgIDList = ReturnDistinctCANMsgIds(Buses.CANBusOrange, CANMsgIdList);

                #endregion


                private List<string> ReturnDistinctCANMsgIds(Buses bus, List<CANBusMsgIdMap> CANMsgIdList)
            {
                return (from CANBusMsgIdMap busIdMap in CANMsgIdList
                        where busIdMap.Bus == bus
                        select busIdMap.MsgId).Distinct().ToList();
            }




        }
    }
}


XML
I am using Distinct()method to separate messages and ToList() to list them into list<string>.

I defined List<string> for CANBusMsgIDList and defined objects of comon classes for CANBusRedDetails,CANBusYellowDetails,CANBusGreenDetails.

Now I am getting error at defining objects of common classes.

Can anyone help me in solving this problem

Thanks
John
AnswerRe: How to save data to objects of common classes Pin
Richard MacCutchan18-Nov-13 0:54
mveRichard MacCutchan18-Nov-13 0:54 
QuestionBioPDF multiple printing in C# Pin
Bulgarin17-Nov-13 7:58
Bulgarin17-Nov-13 7:58 
AnswerRe: BioPDF multiple printing in C# Pin
Dave Kreskowiak17-Nov-13 8:46
mveDave Kreskowiak17-Nov-13 8:46 
GeneralRe: BioPDF multiple printing in C# Pin
Bulgarin17-Nov-13 19:48
Bulgarin17-Nov-13 19:48 
GeneralRe: BioPDF multiple printing in C# Pin
Mycroft Holmes17-Nov-13 20:42
professionalMycroft Holmes17-Nov-13 20:42 
GeneralRe: BioPDF multiple printing in C# Pin
Bulgarin17-Nov-13 21:16
Bulgarin17-Nov-13 21:16 
GeneralRe: BioPDF multiple printing in C# Pin
Marco Bertschi17-Nov-13 21:50
protectorMarco Bertschi17-Nov-13 21:50 
GeneralRe: BioPDF multiple printing in C# Pin
Bulgarin17-Nov-13 21:59
Bulgarin17-Nov-13 21:59 
GeneralRe: BioPDF multiple printing in C# Pin
Marco Bertschi17-Nov-13 23:13
protectorMarco Bertschi17-Nov-13 23:13 
GeneralRe: BioPDF multiple printing in C# Pin
Bulgarin17-Nov-13 23:25
Bulgarin17-Nov-13 23:25 
QuestionMessage Closed Pin
16-Nov-13 10:30
professionalGengou16-Nov-13 10:30 
AnswerRe: Hungarian Algorithm Pin
Richard Andrew x6416-Nov-13 10:42
professionalRichard Andrew x6416-Nov-13 10:42 
GeneralMessage Closed Pin
16-Nov-13 11:22
professionalGengou16-Nov-13 11:22 
GeneralRe: Hungarian Algorithm Pin
Richard Andrew x6416-Nov-13 11:24
professionalRichard Andrew x6416-Nov-13 11:24 
GeneralRe: Hungarian Algorithm Pin
Gengou16-Nov-13 11:28
professionalGengou16-Nov-13 11:28 
GeneralRe: Hungarian Algorithm Pin
Richard Andrew x6416-Nov-13 11:30
professionalRichard Andrew x6416-Nov-13 11:30 
GeneralRe: Hungarian Algorithm Pin
Gengou16-Nov-13 11:32
professionalGengou16-Nov-13 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.