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. An object reference is required for the nonstatic field, method, or property .... why??

An object reference is required for the nonstatic field, method, or property .... why??

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 5 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.
  • P Offline
    P Offline
    Ponzano Paolo
    wrote on last edited by
    #1

    hello, I'm trying to take a string from a dialog box and pass it to another form, here's my code password.cs ... private String tmpPassStore; ... private void pass_ok_Click(object sender, System.EventArgs e) { tmpPassStore=this.passText.Text; Form1.changePass(tmpPassStore); } form1.cs ... public String DBPassword ; ... public void changePass(String pwd) { this.DBPassword=pwd; } but at compile time, I receive the following error: D:\Projects\myBusinnes\password.cs(120): An object reference is required for the nonstatic field, method, or property 'myBusinnes.Form1.changePass(string)' why??? I pass an object of type string... Thanks in advance Paolo

    S 1 Reply Last reply
    0
    • P Ponzano Paolo

      hello, I'm trying to take a string from a dialog box and pass it to another form, here's my code password.cs ... private String tmpPassStore; ... private void pass_ok_Click(object sender, System.EventArgs e) { tmpPassStore=this.passText.Text; Form1.changePass(tmpPassStore); } form1.cs ... public String DBPassword ; ... public void changePass(String pwd) { this.DBPassword=pwd; } but at compile time, I receive the following error: D:\Projects\myBusinnes\password.cs(120): An object reference is required for the nonstatic field, method, or property 'myBusinnes.Form1.changePass(string)' why??? I pass an object of type string... Thanks in advance Paolo

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      The compiler doesn't complain about the string. To call the nonstatic method "changePass" of your "Form1" class you need an object of this class.


      www.troschuetz.de

      P 1 Reply Last reply
      0
      • S Stefan Troschuetz

        The compiler doesn't complain about the string. To call the nonstatic method "changePass" of your "Form1" class you need an object of this class.


        www.troschuetz.de

        P Offline
        P Offline
        Paolo Ponzano
        wrote on last edited by
        #3

        and excuse me how do I fix that?? Form1 calls password's form creating a new instance of the password object, how do I tell password's one to relay to form1 object?? thanks Paolo

        J D 2 Replies Last reply
        0
        • P Paolo Ponzano

          and excuse me how do I fix that?? Form1 calls password's form creating a new instance of the password object, how do I tell password's one to relay to form1 object?? thanks Paolo

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          2 options: 1) Pass a reference to Form1 into the password form 2) Pass a delegate into the password form allowing callback to Form1

          1 Reply Last reply
          0
          • P Paolo Ponzano

            and excuse me how do I fix that?? Form1 calls password's form creating a new instance of the password object, how do I tell password's one to relay to form1 object?? thanks Paolo

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            It's not working because Form1 doesn't exist in the context of the Password form. "There is no reference to an object of type Form1." In other words, in Form1, it created an object of type Password (form). Form1 knows everything public about the Password object. The Password object, on the other hand, knows nothing of the object that created it (Form1). As a rule of thumb, Parent objects create child objects, know everything about them, and can manipulate them. Child objects know nothing of the parents that created them and, hence, can't directly modify them. 3rd option... Create a Public field on the Password form and have Form1 look at that field and retrieve the string itself. Instead of trying to pass the string from the Password form to Form1, have Form1 pickup the string from the Password form. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            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