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


I am strucking with a error in the following code:

I tried a lot to solve the problem. But I could not.

Can anyone please help me in solving this?
C#
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();
        }

        foreach (CANBusMsgIdMap CANBusMsgIdMap in CANBusGreenDetails.CANBusMsgIDList)
            {
                Console.WriteLine(CANBusGreenDetails.CANBusMsgIDList);


            }

            for (double i = 0; i <= 10000000000000; i++)
            {
            }
     
            




        }
    }

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


Thanks
John
Posted
Updated 18-Nov-13 4:35am
v4
Comments
agent_kruger 18-Nov-13 9:24am    
please mention in which line it shows the error? and what is "CANBusDetails" ?
Member 10408451 18-Nov-13 9:27am    
Error is at 71,74,77,80 and all are same errors
ZurdoDev 18-Nov-13 10:06am    
Reply to comment so user is notified instead of adding a comment to your own question.
ZurdoDev 18-Nov-13 10:06am    
And since you did not include the line numbers in what you posted, how can we help?
Mike Meinz 18-Nov-13 9:38am    
It looks like you never instantiate CANBusRedDetails, CANBusYellowDetails, CANBusGreenDetails or CANBusOrangeDetails. You must instantiate them before you use them.

Main is static but is calling Bar that is not. That won't work.

Either new up an instance of Program and call Bar on that;
C#
static void Main()
{
  var p = new Program();
  p.Bar();
}

Or make Bar static.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
phil.o 18-Nov-13 10:40am    
5'd :)
Fredrik Bornander 18-Nov-13 10:41am    
Dude, that is the fastest +5 I've ever seen! Kudos to you, Sir!
You must instantiate CANBusRedDetails, CANBusYellowDetails, CANBusGreenDetails and CANBusOrangeDetails before you use them.


C#
CANBusRedDetails = new CANBusDetails; 
CANBusYellowDetails = new CANBusDetails; 
CANBusGreenDetails = new CANBusDetails; 
CANBusOrangeDetails = new CANBusDetails;
 
Share this answer
 

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