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. Object Reference Required (CS0120) Form Access

Object Reference Required (CS0120) Form Access

Scheduled Pinned Locked Moved C#
csharpperformancehelpquestion
5 Posts 2 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.
  • C Offline
    C Offline
    codemunkeh
    wrote on last edited by
    #1

    OK, so I've recently picked up C#. Actually, I knew Jack about it until I dumped some VB code into it, ran some regexs, cleaned up the errors and have finally whittled it down to a total of 16 errors. I fixed 12 of the "Object reference is required for the nonstatic field, method or property [blah]" but there's 4 left. In VB it works great with: frmMain.pnlDisplay.Invalidate() but to fix the CS0120, MSDN basically recommends replacing it with "new frmMain()". Yeah, great, what that does is creates an infinite loop of new forms being created within one another. It hasn't overloaded any stacks yet but I do have heaps (bad pun) of memory. Basically I'd like to know how I can access a control on a form from within another class, without having to create new instances and preferably without having to pass references to "this" around everywhere. Or can I get away with in the constructor only?


    Ninja (the Nerd)
    Confused? You will be...

    A 1 Reply Last reply
    0
    • C codemunkeh

      OK, so I've recently picked up C#. Actually, I knew Jack about it until I dumped some VB code into it, ran some regexs, cleaned up the errors and have finally whittled it down to a total of 16 errors. I fixed 12 of the "Object reference is required for the nonstatic field, method or property [blah]" but there's 4 left. In VB it works great with: frmMain.pnlDisplay.Invalidate() but to fix the CS0120, MSDN basically recommends replacing it with "new frmMain()". Yeah, great, what that does is creates an infinite loop of new forms being created within one another. It hasn't overloaded any stacks yet but I do have heaps (bad pun) of memory. Basically I'd like to know how I can access a control on a form from within another class, without having to create new instances and preferably without having to pass references to "this" around everywhere. Or can I get away with in the constructor only?


      Ninja (the Nerd)
      Confused? You will be...

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      Make a method in the other class that accesses its own controls, then call the method from where ever you need to.

      My current favourite word is: Waffle Cheese is still good though.

      C 2 Replies Last reply
      0
      • A Anthony Mushrow

        Make a method in the other class that accesses its own controls, then call the method from where ever you need to.

        My current favourite word is: Waffle Cheese is still good though.

        C Offline
        C Offline
        codemunkeh
        wrote on last edited by
        #3

        Would seem logical I suppose...:doh: Thanks Wish I'd stuck with VB. *thinks* Wish I knew how to work C# better.


        Ninja (the Nerd)
        Confused? You will be...

        1 Reply Last reply
        0
        • A Anthony Mushrow

          Make a method in the other class that accesses its own controls, then call the method from where ever you need to.

          My current favourite word is: Waffle Cheese is still good though.

          C Offline
          C Offline
          codemunkeh
          wrote on last edited by
          #4

          Perhaps not. Double post, yeah, but I've only just put the change in. It now reads "Object reference required for nonstatic [...] 'frmMain.paintDisplay()'"


          Ninja (the Nerd)
          Confused? You will be...

          A 1 Reply Last reply
          0
          • C codemunkeh

            Perhaps not. Double post, yeah, but I've only just put the change in. It now reads "Object reference required for nonstatic [...] 'frmMain.paintDisplay()'"


            Ninja (the Nerd)
            Confused? You will be...

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            Are you trying to call this stuff from a static method? Because if so, that'll be the problem. You'd need to make the method not static, or make everything else inside the method static. Or wait, if your using a static method, pass frmMain as a parameter, and then it should work: static void myMethod(Form myForm) { myForm.paintDisplay(); } ... myMethod(frmMain) and if your method isn't static, then im not sure. Keep at it though.

            My current favourite word is: Waffle Cheese is still good though.

            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