why I cannot read the data of log file using constructor?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1459 Views - Last Post: 19 November 2013 - 12:29 AM Rate Topic: -----

#1 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 06:50 AM

Hi,

I am trying to read the log file data and store the msgID and Bus number using constructor

But while displaying in console it is showing something as I attached in Image

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication13
{
    #region Buses

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

    #endregion

    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;
            // return this.Bus + ", " + this.msgId;
        }
        //public override string ToString() // Equals,GetHash also there.here I am using ToString
        //{
        //    return this.CANBus + ", " + this.CANMsgId;
        //}
    }



    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";
            //String seclogPath1 = @"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\1302_P3\text.txt";
            //String seclogPath1 = @"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\1302_P3\Red_SOP1302_P3.xls";




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

                    Console.WriteLine(CANMsgIdList);
                    for (double i = 0; i <= 1000000000; i++)
                    {
                    }

                }

            }






        }
    }
}


Can any one suggest me how can I see the data inside CANMsgIdList


Thanks
John

Attached image(s)

  • Attached Image

This post has been edited by vivek20055: 16 November 2013 - 06:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: why I cannot read the data of log file using constructor?

#2 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 06:59 AM

The reason you are getting this is because CANMsgIdList is a List, something similar to array and to print that you need to loop through that list and print each element separately.

I bit off topic but why are you ignoring the advice people give you in several previous posts, please read a basics tutorial here on DIC or find some kind of guided system like a book or teacher if you are in school.

This post has been edited by Michael26: 16 November 2013 - 07:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 07:12 AM

View PostMichael26, on 16 November 2013 - 06:59 AM, said:

The reason you are getting this is because CANMsgIdList is a List, something similar to array and to print that you need to loop through that list and print each element separately.

I bit off topic but why are you ignoring the advice people give you in several previous posts, please read a basics tutorial here on DIC or find some kind of guided system like a book or teacher if you are in school.


Hi,

I tried to print each element of the array like this

Console.WriteLine(CANMsgIdList[0]);


But I am still getting the result like this

I am not getting the data inside the array

How can I see the data inside the array

Thanks
John

View Postvivek20055, on 16 November 2013 - 07:07 AM, said:

View PostMichael26, on 16 November 2013 - 06:59 AM, said:

The reason you are getting this is because CANMsgIdList is a List, something similar to array and to print that you need to loop through that list and print each element separately.

I bit off topic but why are you ignoring the advice people give you in several previous posts, please read a basics tutorial here on DIC or find some kind of guided system like a book or teacher if you are in school.


Hi,

I tried to print each element of the array like this

Console.WriteLine(CANMsgIdList[0]);


But I am still getting the result like this

I am not getting the data inside the array

How can I see the data inside the array

Thanks
John



I tried using override inside the constructor. But still showing error inside the constructor

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 07:32 AM

Use for loop, Console.WriteLine(CANMsgIdList[0]); print the first element.
Was This Post Helpful? 0
  • +
  • -

#5 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 07:37 AM

View PostMichael26, on 16 November 2013 - 07:32 AM, said:

Use for loop, Console.WriteLine(CANMsgIdList[0]); print the first element.


Hi,

I tried using for loop as u said

I am getting something like in the image

for (int j = 0; j <= 10; j++)
            {
                Console.WriteLine(CANMsgIdList[0]);
            }

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 07:43 AM

for (int j = 0; j <= 10; j++)
            {
                Console.WriteLine(CANMsgIdList[j]);
            }


you need to know that loop are repeating statements inside, so if you do this
for (int j = 0; j <= 10; j++)
            {
                Console.WriteLine(CANMsgIdList[0]);
            }

that will print first element 10 times
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 07:54 AM

You will also be printing instances of CANBusMsgIdMap, which won't produce any meaningful output unless you override the ToString() method in the Class - which you started to do.

..and you will be attempting to print 11 elements.

This post has been edited by andrewsw: 16 November 2013 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6496
  • View blog
  • Posts: 22,229
  • Joined: 05-May 12

Re: why I cannot read the data of log file using constructor?

Posted 16 November 2013 - 10:07 AM

Also as an aside: Try to keep constructors simple and focused on just initializing the class. Doing major work should be in a method, rather than a constructor.
Was This Post Helpful? 1
  • +
  • -

#9 mavarazo   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 12:01 AM

What should the Console print out? The collection is full of CANBusMsgIdMap objects. So if u loop over the collection and print out the object, u wont get any usefull infos.

Try to define the property u want to have displayed in the console or override ToString () in the CANBusMsgIdMap class.
Was This Post Helpful? 0
  • +
  • -

#10 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 02:37 AM

Hi,

Thank you for your suggestions, I am able to read the data using the constructor.

And the outpur is as shown in the image.

Now I want to separate the messages of RED,YELLOW and GREEN.

I am trying something like this in the following code.

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




        }
    }
}


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

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6496
  • View blog
  • Posts: 22,229
  • Joined: 05-May 12

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 07:12 AM

What is the exact error you are getting? Can you show us the code that is generating that error and which line it is happening on?

[Your code organization, naming convention, and indentation is atrocious. I'm sorry if that hurts your feelings. I've looked at multi-thousand line code posted here in DIC before. I usually don't ask people for line numbers for errors, but the code you posted made my eyes want to glaze over and you didn't even have 200 lines. I still want to try to help, hence the request for specifics.]
Was This Post Helpful? 0
  • +
  • -

#12 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 07:19 AM

View PostSkydiver, on 18 November 2013 - 07:12 AM, said:

What is the exact error you are getting? Can you show us the code that is generating that error and which line it is happening on?

[Your code organization, naming convention, and indentation is atrocious. I'm sorry if that hurts your feelings. I've looked at multi-thousand line code posted here in DIC before. I usually don't ask people for line numbers for errors, but the code you posted made my eyes want to glaze over and you didn't even have 200 lines. I still want to try to help, hence the request for specifics.]


Hi,

Sorry about that

I did not wrote my code in organized way

Now I modified it and made it simpler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace SCANLA
{
    class Program
    {
      

       
        public static CANBusDetails CANBusRedDetails = null; 
        public static CANBusDetails CANBusYellowDetails = null;
        public static CANBusDetails CANBusGreenDetails = null;
        public static CANBusDetails CANBusOrangeDetails = null;
        
      

       

        static void Main(string[] args)
        {

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

            
            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 (lineWords.Length < (2 + 1))
                    continue;

              
                if (lineWords[2].EndsWith("x"))
                    CANMsgId = lineWords[2].TrimEnd('x');
                else
                    continue;

              
                if (Regex.IsMatch(CANMsgId, @"^[0-9A-Fa-f]+$"))



                {
                    Buses CANBus = (Buses)Enum.Parse(typeof(Buses), (lineWords[1]));

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

                    

                }

            }
            

            // 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);

           


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

     
            




        }
    }




Error 2 An object reference is required for the non-static field, method, or property

Error is at line number 71,74,76,79 and all of same type

Can you please help me in solving this


Thanks
John

This post has been edited by vivek20055: 18 November 2013 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 07:22 AM

Please do not start a duplicate of this topic. I've closed your other one.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6496
  • View blog
  • Posts: 22,229
  • Joined: 05-May 12

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 07:31 AM

You are calling your instance method ReturnDistinctCANMsgIds from within a static method. You need an instance in order to call an instance method.

It's like you are trying to do the following:
class Foo
{
    static void Main()
    {
        Bar();
    }

    void Bar()
    {
    }
}



The quick fix is to make Bar() static. BUT that is not the good long term fix because it makes testing and refactoring the code much harder. The correct solution is to create an instance of Foo and invoke Bar().
Was This Post Helpful? 0
  • +
  • -

#15 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: why I cannot read the data of log file using constructor?

Posted 18 November 2013 - 08:11 AM

View PostSkydiver, on 18 November 2013 - 07:31 AM, said:

You are calling your instance method ReturnDistinctCANMsgIds from within a static method. You need an instance in order to call an instance method.

It's like you are trying to do the following:
class Foo
{
    static void Main()
    {
        Bar();
    }

    void Bar()
    {
    }
}



The quick fix is to make Bar() static. BUT that is not the good long term fix because it makes testing and refactoring the code much harder. The correct solution is to create an instance of Foo and invoke Bar().





Hi,

I tried it and it is again showing error at Bar();

Error 2 An object reference is required for the non-static field, method, or property 'SCANLA.Program.Bar()'



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace SCANLA
{
    class Program
    {
      

       
        public static CANBusDetails CANBusRedDetails = null; 
        public static CANBusDetails CANBusYellowDetails = null;
        public static CANBusDetails CANBusGreenDetails = null;
        public static CANBusDetails CANBusOrangeDetails = null;
        

       

       

        static void Main()
        {
              
            Bar();
        }



        void Bar()
        {




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


            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 (lineWords.Length < (2 + 1))
                    continue;


                if (lineWords[2].EndsWith("x"))
                    CANMsgId = lineWords[2].TrimEnd('x');
                else
                    continue;


                if (Regex.IsMatch(CANMsgId, @"^[0-9A-Fa-f]+$"))
                {
                    Buses CANBus = (Buses)Enum.Parse(typeof(Buses), (lineWords[1]));

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



                }

            }


            // 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);
        

        


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

     
            




        }
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2