Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event handling Object reference error

Event handling Object reference error

Scheduled Pinned Locked Moved C#
helpjsonannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gwithey
    wrote on last edited by
    #1

    I am trying to create an on update event and have recieved the following error: An object reference is required for the non-static field, method, or property 'RenishawVMCU.CommandParser.OnUpdate(RenishawVMCU.CommandData)' It is generated form this line: OnUpdate(rxPortData);

    public class CommandParser
    {
    CommandData rxPortData = new CommandData();

    public delegate void delUpdate(CommandData commandData);
    public event delUpdate update;

      protected virtual void OnUpdate(CommandData commandData)
      {
         if (update != null)
         {
            this.update(commandData);
         }
      }
    

    }
    public static string ParseCommand(string m_recievedUCCData)
    {
    byte[] rxData;

         rxData = StrToByteArray(m\_recievedUCCData);
    
         //CommandData rxPortData = new CommandData();
         CommandData rxPortData = ParseCommand(rxData);
    
         //OnUpdate(rxPortData);
    
         rxData.ToString();
         return "Parsing done!" ;
        
      }
    

    public class VirtualMCU
    {
    public VirtualMCU()
    {
    cmdParser.update += new CommandParser.delUpdate(cmdParser_update);
    }
    }

    Does anyone know what could be wrong as this is the only line that is causing a problem i cant work it out.

    U D L 3 Replies Last reply
    0
    • G gwithey

      I am trying to create an on update event and have recieved the following error: An object reference is required for the non-static field, method, or property 'RenishawVMCU.CommandParser.OnUpdate(RenishawVMCU.CommandData)' It is generated form this line: OnUpdate(rxPortData);

      public class CommandParser
      {
      CommandData rxPortData = new CommandData();

      public delegate void delUpdate(CommandData commandData);
      public event delUpdate update;

        protected virtual void OnUpdate(CommandData commandData)
        {
           if (update != null)
           {
              this.update(commandData);
           }
        }
      

      }
      public static string ParseCommand(string m_recievedUCCData)
      {
      byte[] rxData;

           rxData = StrToByteArray(m\_recievedUCCData);
      
           //CommandData rxPortData = new CommandData();
           CommandData rxPortData = ParseCommand(rxData);
      
           //OnUpdate(rxPortData);
      
           rxData.ToString();
           return "Parsing done!" ;
          
        }
      

      public class VirtualMCU
      {
      public VirtualMCU()
      {
      cmdParser.update += new CommandParser.delUpdate(cmdParser_update);
      }
      }

      Does anyone know what could be wrong as this is the only line that is causing a problem i cant work it out.

      U Offline
      U Offline
      Uri Lavi
      wrote on last edited by
      #2

      ParseCommand is a static method. You cannot access non static methods / members from the static one.

      Uri

      1 Reply Last reply
      0
      • G gwithey

        I am trying to create an on update event and have recieved the following error: An object reference is required for the non-static field, method, or property 'RenishawVMCU.CommandParser.OnUpdate(RenishawVMCU.CommandData)' It is generated form this line: OnUpdate(rxPortData);

        public class CommandParser
        {
        CommandData rxPortData = new CommandData();

        public delegate void delUpdate(CommandData commandData);
        public event delUpdate update;

          protected virtual void OnUpdate(CommandData commandData)
          {
             if (update != null)
             {
                this.update(commandData);
             }
          }
        

        }
        public static string ParseCommand(string m_recievedUCCData)
        {
        byte[] rxData;

             rxData = StrToByteArray(m\_recievedUCCData);
        
             //CommandData rxPortData = new CommandData();
             CommandData rxPortData = ParseCommand(rxData);
        
             //OnUpdate(rxPortData);
        
             rxData.ToString();
             return "Parsing done!" ;
            
          }
        

        public class VirtualMCU
        {
        public VirtualMCU()
        {
        cmdParser.update += new CommandParser.delUpdate(cmdParser_update);
        }
        }

        Does anyone know what could be wrong as this is the only line that is causing a problem i cant work it out.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        This is static

        public static string ParseCommand(string m_recievedUCCData)
        {
        // ...
        OnUpdate(rxPortData);
        // ...
        }

        This is not so can only be called by an instance of CommandParser

        protected virtual void OnUpdate(CommandData commandData)
        {
        // ...
        }

        If there is no particular reason for the ParseCommand method to be static, then make it an instance member. Alternatively, pass a CommandParser instance as a parameter into the ParseCommand method and act on that.

        public static string ParseCommand(string m_recievedUCCData, CommandParser commandParser)
        {
        // ...
        commandParser.OnUpdate(rxPortData);
        // ...
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        G 1 Reply Last reply
        0
        • D DaveyM69

          This is static

          public static string ParseCommand(string m_recievedUCCData)
          {
          // ...
          OnUpdate(rxPortData);
          // ...
          }

          This is not so can only be called by an instance of CommandParser

          protected virtual void OnUpdate(CommandData commandData)
          {
          // ...
          }

          If there is no particular reason for the ParseCommand method to be static, then make it an instance member. Alternatively, pass a CommandParser instance as a parameter into the ParseCommand method and act on that.

          public static string ParseCommand(string m_recievedUCCData, CommandParser commandParser)
          {
          // ...
          commandParser.OnUpdate(rxPortData);
          // ...
          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          G Offline
          G Offline
          gwithey
          wrote on last edited by
          #4

          Thanx for the help. I had not yet learnt of this so was useful information. i soleved the probelm by passing in the perameter as you suggested which stopped the error. This is the final code, it didnt have a problem with calling Parsecommand(byte[]) as the method is static.

          public static string ParseCommand(string m_recievedUCCData, CommandParser commandParser)
          {
          byte[] rxData;

               rxData = StrToByteArray(m\_recievedUCCData);
          
               CommandData rxPortData = ParseCommand(rxData);
          
               commandParser.OnUpdate(rxPortData);
          
               //rxData.ToString();
               return "Parsing done!" ;
              
            }
          
          1 Reply Last reply
          0
          • G gwithey

            I am trying to create an on update event and have recieved the following error: An object reference is required for the non-static field, method, or property 'RenishawVMCU.CommandParser.OnUpdate(RenishawVMCU.CommandData)' It is generated form this line: OnUpdate(rxPortData);

            public class CommandParser
            {
            CommandData rxPortData = new CommandData();

            public delegate void delUpdate(CommandData commandData);
            public event delUpdate update;

              protected virtual void OnUpdate(CommandData commandData)
              {
                 if (update != null)
                 {
                    this.update(commandData);
                 }
              }
            

            }
            public static string ParseCommand(string m_recievedUCCData)
            {
            byte[] rxData;

                 rxData = StrToByteArray(m\_recievedUCCData);
            
                 //CommandData rxPortData = new CommandData();
                 CommandData rxPortData = ParseCommand(rxData);
            
                 //OnUpdate(rxPortData);
            
                 rxData.ToString();
                 return "Parsing done!" ;
                
              }
            

            public class VirtualMCU
            {
            public VirtualMCU()
            {
            cmdParser.update += new CommandParser.delUpdate(cmdParser_update);
            }
            }

            Does anyone know what could be wrong as this is the only line that is causing a problem i cant work it out.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Here[^] is a hint for you. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups