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

I have a method in which I am passing arguments to a constructor and the method returns the generic.list.collection when the method is called

Now I am calling this method in main method. I need compare one of the arguments of generic.list.collection with another variable in main method.

How can I do this??. How can I take one argument of constructor out of three in my program to compare???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;
//using SCANLA.Common;
using System.Text.RegularExpressions;

namespace ConsoleApplication13

    class Program

        static void Main()
            var p = new Program();

        void Bar()
            List<MsgIdTimeStampMap> CANMsgIdList = new List<MsgIdTimeStampMap>();
           CANMsgIdList=  ReadLogFile(@"\\\home\se\121\valhbc\Desktop\log files\log file with orange\logfile.asc");

        private List<MsgIdTimeStampMap> ReadLogFile(string dataLogFile)
            string[] fileContents = null;
            List<MsgIdTimeStampMap> CANMsgIdList = new List<MsgIdTimeStampMap>();

                // Open and read the entire Data Log File contents.
                fileContents = File.ReadAllLines( dataLogFile);
            catch (Exception ex)
                throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);

            if (fileContents == null)
                throw new Exception("The Input log file was not found or is not in correct format");

            // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
            for (int Index = 0; Index < fileContents.Length; Index++)
                string CANMsgId = string.Empty;
                string timeStamp = 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 < (Constants.CAN_MSGID_HEX_INDEX + 1))

                // 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[Constants.CAN_MSGID_HEX_INDEX].EndsWith("x"))
                    CANMsgId = lineWords[Constants.CAN_MSGID_HEX_INDEX].TrimEnd('x');
                    timeStamp = lineWords[Constants.CAN_TIMESTAMP_INDEX];

                // Check the format of the CAN Msg Id, whether Hex or not.
                if (Regex.IsMatch(CANMsgId, Constants.CAN_MSGID_HEX_FORMAT_REGREX))
                    Buses CANBus = (Buses)Enum.Parse(typeof(Buses), (lineWords[Constants.CAN_BUSID_INDEX]));
                    CANMsgIdList.Add(new MsgIdTimeStampMap(CANBus, CANMsgId, timeStamp));

            return CANMsgIdList;




this is my constructor

public class MsgIdTimeStampMap
            string msgId;
            string timeStamp;
            public Buses Bus { get; set; }

            public string MsgId
                    if (!String.IsNullOrEmpty(msgId))
                        return msgId.ToUpper();
                        return string.Empty;

                set { msgId = value; }

            public string TimeStamp
                    if (!String.IsNullOrEmpty(timeStamp))
                        return timeStamp;
                        return string.Empty;

                set { timeStamp = value; }

            public MsgIdTimeStampMap(Buses bus, string msgId, string timeStamp)
                this.Bus = bus;
                this.msgId = msgId;
                this.timeStamp = timeStamp;

            public override string ToString() // Equals,GetHash also I am using ToString
                return this.MsgId + ", " + this.Bus + ", " + this.TimeStamp;


In this code ReadLogFile method is called in main method and ReadLogFile method returns 3 arguments of constructor to main method. In the main method how can I access one argument of Constructor for comparing???

Updated 19-Dec-13 8:06am
Alexander Dymshyts 19-Dec-13 13:36pm
Can you provide your code?
BillWoodruff 19-Dec-13 13:57pm
We need much more specific information, and to see your code.

It appears you are trying to catch the return values in CANMsgIdList in the Bar() method. though it doesn't appear you have declared it yet in the class or the Bar() method.

Once you catch it you can iterate through the list with a foreach or a for i and do your comparisons. If you only want the 2nd argument (or specific) you can also reference it CANMsgIdList[1] as well.
Member 10408451 20-Dec-13 7:15am
Hi thanks for your reply
CANMsgIdList[0],CANMsgIdList[2],CANMsgIdList[3]...etc gives me the data of different lines of log file but not the individual arguments
But how can I access individual argument
bowlturner 20-Dec-13 9:01am

Right now you declare, and instantiate, CANMsgIdList in the 'Bar method:
List<MsgIdTimeStampMap> CANMsgIdList = new List<msgidtimestampmap>();</msgidtimestampmap>
So CANMsgIdList is a private variable that will never be accessible outside the scope of 'Bar.

You also declare, and instantiate, a variable with the same name in the 'ReadLogFile method:
List<MsgIdTimeStampMap> CANMsgIdList = new List<MsgIdTimeStampMap>();
That variable will not be accessible outside the scope of 'ReadLogFile.

While the C# compiler will not throw an error if it finds variables of the same Types, and Names, in different lexical scopes (and ReSharper won't flag it either), having that kind of duplication is, imho, a recipe for confusing code, and future errors.

If you want to access the instance of CANMsgIdList in your Main procedure, or in other methods in your Application, declare, and instantiate, it once in the scope of the Program/Class.

For example:
// in Program/Class scope:
List<MsgIdTimeStampMap> CANMsgIdList;

private void Bar()
    // initialize CANMsgIDList
    CANMsgIdList = new List<msgidtimestampmap>();

      files\log file with orange\logfile.asc");
If you go this route you can then modify the 'ReadLogFile method to return void:
private void ReadLogFile(string dataLogFile)
and eliminate the return statement in the method: now the instance 'Add method in 'ReadLogFile will operate directly on the instance of CANMsgList available to all methods in the Program/Class.

However, that's just one approach: there would be nothing wrong with passing the instance of 'CANMsgIdListas a parameter into 'ReadLogFile, and having 'ReadLogFile return the same instance after it has been "fleshed-out."

The extent of "formality" in passing in structures as parameters, and returning them, is most often influenced by the extent you need to re-use methods.

It's a very good idea, imho, to define Type access modifiers as you create methods and fields, structs, etc. It makes your code much more readable, and helps highlight issues related to scope, like the issue here. 'public and 'private cost you very little :)

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