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. C# error: “An object reference is required for the non-static field, method, or property”

C# error: “An object reference is required for the non-static field, method, or property”

Scheduled Pinned Locked Moved C#
csharphelp
17 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.
  • W Offline
    W Offline
    wrightyrx7
    wrote on last edited by
    #1

    Im trying to figure out Delegates as I have read this is the best way to pass Values from one form to another. I have tried to do this but am getting an error. So to test I am trying to do the following. Form1 has a button which opens Form2 which contains a DatePicker. The user selects a date and clicks OK. I then want the date passed back to a textbox on Form1. This is my code. FORM1 - CODE (The getEffDate in this code is where im getting the error.)

    private void TestingButton_Click(object sender, EventArgs e)
    {
    Form_EffectiveDate effDate = new Form_EffectiveDate();
    if (effDate.ShowDialog() == DialogResult.OK)
    {
    EffDateTextBox.Text = Form_EffectiveDate.getEffDate;
    }
    }

    FORM 2 - Code

    namespace Manager_Changes
    {
    public partial class Form_EffectiveDate : Form
    {
    public delegate void GetEffDate(object sender);
    public GetEffDate getEffDate;

        public Form\_EffectiveDate()
        {
            InitializeComponent();
        }
    
        private void DateOKButton\_Click(object sender, EventArgs e)
        {
            getEffDate(effDateTimePicker.Value.ToString());
            DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
    

    }

    Chris Wright

    OriginalGriffO P S 3 Replies Last reply
    0
    • W wrightyrx7

      Im trying to figure out Delegates as I have read this is the best way to pass Values from one form to another. I have tried to do this but am getting an error. So to test I am trying to do the following. Form1 has a button which opens Form2 which contains a DatePicker. The user selects a date and clicks OK. I then want the date passed back to a textbox on Form1. This is my code. FORM1 - CODE (The getEffDate in this code is where im getting the error.)

      private void TestingButton_Click(object sender, EventArgs e)
      {
      Form_EffectiveDate effDate = new Form_EffectiveDate();
      if (effDate.ShowDialog() == DialogResult.OK)
      {
      EffDateTextBox.Text = Form_EffectiveDate.getEffDate;
      }
      }

      FORM 2 - Code

      namespace Manager_Changes
      {
      public partial class Form_EffectiveDate : Form
      {
      public delegate void GetEffDate(object sender);
      public GetEffDate getEffDate;

          public Form\_EffectiveDate()
          {
              InitializeComponent();
          }
      
          private void DateOKButton\_Click(object sender, EventArgs e)
          {
              getEffDate(effDateTimePicker.Value.ToString());
              DialogResult = System.Windows.Forms.DialogResult.OK;
              this.Close();
          }
      }
      

      }

      Chris Wright

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Delegates aren't "the best way" to pass variables back, Events are (which use delegates, but hides the complexities from you). Have a look here: Transferring information between two forms, Part 2: Child to Parent[^]

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      W 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Delegates aren't "the best way" to pass variables back, Events are (which use delegates, but hides the complexities from you). Have a look here: Transferring information between two forms, Part 2: Child to Parent[^]

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        W Offline
        W Offline
        wrightyrx7
        wrote on last edited by
        #3

        Perfect! I will get researching again. I appreciate the information, thanks @DalekDave. As I am new to C# its hard to tell if someone is right when they say "the best way" as that might just be personal preference. Thanks again, have a great day.

        Chris Wright

        OriginalGriffO 1 Reply Last reply
        0
        • W wrightyrx7

          Im trying to figure out Delegates as I have read this is the best way to pass Values from one form to another. I have tried to do this but am getting an error. So to test I am trying to do the following. Form1 has a button which opens Form2 which contains a DatePicker. The user selects a date and clicks OK. I then want the date passed back to a textbox on Form1. This is my code. FORM1 - CODE (The getEffDate in this code is where im getting the error.)

          private void TestingButton_Click(object sender, EventArgs e)
          {
          Form_EffectiveDate effDate = new Form_EffectiveDate();
          if (effDate.ShowDialog() == DialogResult.OK)
          {
          EffDateTextBox.Text = Form_EffectiveDate.getEffDate;
          }
          }

          FORM 2 - Code

          namespace Manager_Changes
          {
          public partial class Form_EffectiveDate : Form
          {
          public delegate void GetEffDate(object sender);
          public GetEffDate getEffDate;

              public Form\_EffectiveDate()
              {
                  InitializeComponent();
              }
          
              private void DateOKButton\_Click(object sender, EventArgs e)
              {
                  getEffDate(effDateTimePicker.Value.ToString());
                  DialogResult = System.Windows.Forms.DialogResult.OK;
                  this.Close();
              }
          }
          

          }

          Chris Wright

          P Offline
          P Offline
          phil o
          wrote on last edited by
          #4

          I usually do it this way:

          private void TestingButton_Click(object sender, EventArgs e)
          {
          using (Form_EffectiveDate form = new Form_EffectiveDate())
          {
          if (form.ShowDialog(this) == DialogResult.OK)
          {
          EffDateTextBox.Text = form.EffectiveDate;
          }
          }
          }

          In Form_EffectiveDate, set the OKButton property of the form to DateOKButton (this negates the need to write an event-handler for the button). Then:

          public partial class Form_EffectiveDate : Form
          {
          public DateTime EffectiveDate
          {
          get { return effDateTimePicker.Value; }
          }

          public Form\_EffectiveDate()
          {
              InitializeComponent();
          }
          

          }

          enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }

          W 1 Reply Last reply
          0
          • W wrightyrx7

            Perfect! I will get researching again. I appreciate the information, thanks @DalekDave. As I am new to C# its hard to tell if someone is right when they say "the best way" as that might just be personal preference. Thanks again, have a great day.

            Chris Wright

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            I'm not DalekDave - I'm OriginalGriff. DalekDave is someone I'm winding up a little by automatically sending him an email every time I post something! :laugh:

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            W 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              I'm not DalekDave - I'm OriginalGriff. DalekDave is someone I'm winding up a little by automatically sending him an email every time I post something! :laugh:

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              W Offline
              W Offline
              wrightyrx7
              wrote on last edited by
              #6

              Apologies, this is my first time using Code Project. Its not structured like a normal forum haha

              Chris Wright

              OriginalGriffO 1 Reply Last reply
              0
              • P phil o

                I usually do it this way:

                private void TestingButton_Click(object sender, EventArgs e)
                {
                using (Form_EffectiveDate form = new Form_EffectiveDate())
                {
                if (form.ShowDialog(this) == DialogResult.OK)
                {
                EffDateTextBox.Text = form.EffectiveDate;
                }
                }
                }

                In Form_EffectiveDate, set the OKButton property of the form to DateOKButton (this negates the need to write an event-handler for the button). Then:

                public partial class Form_EffectiveDate : Form
                {
                public DateTime EffectiveDate
                {
                get { return effDateTimePicker.Value; }
                }

                public Form\_EffectiveDate()
                {
                    InitializeComponent();
                }
                

                }

                enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }

                W Offline
                W Offline
                wrightyrx7
                wrote on last edited by
                #7

                Thank you, I could give this a go too, it all helps with my learning. Can I ask what you mean by "In Form_EffectiveDate, set the OKButton property of the form to DateOKButton (this negates the need to write an event-handler for the button )"? I clicked the form and looked through the properties but couldn't see anything relating to OKButton.

                Chris Wright

                P 1 Reply Last reply
                0
                • W wrightyrx7

                  Thank you, I could give this a go too, it all helps with my learning. Can I ask what you mean by "In Form_EffectiveDate, set the OKButton property of the form to DateOKButton (this negates the need to write an event-handler for the button )"? I clicked the form and looked through the properties but couldn't see anything relating to OKButton.

                  Chris Wright

                  P Offline
                  P Offline
                  phil o
                  wrote on last edited by
                  #8

                  In the designer view of the form (not the code-file), click on the top of the form. In the properties windows, the form itself will now be selected. Amongst all properties of the form, you have one which is called AcceptButton (my mistake, I gave you a wrong name); set this one to the name of the OK button (you can select it). When that is done, you no longer need to write an event-handler (a method) for the button's click event; the framework will wire up everything and on click of the button the form will be hidden and return DialogResult.OK.

                  enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }

                  W 1 Reply Last reply
                  0
                  • W wrightyrx7

                    Apologies, this is my first time using Code Project. Its not structured like a normal forum haha

                    Chris Wright

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    No problem!

                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    W 1 Reply Last reply
                    0
                    • P phil o

                      In the designer view of the form (not the code-file), click on the top of the form. In the properties windows, the form itself will now be selected. Amongst all properties of the form, you have one which is called AcceptButton (my mistake, I gave you a wrong name); set this one to the name of the OK button (you can select it). When that is done, you no longer need to write an event-handler (a method) for the button's click event; the framework will wire up everything and on click of the button the form will be hidden and return DialogResult.OK.

                      enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }

                      W Offline
                      W Offline
                      wrightyrx7
                      wrote on last edited by
                      #10

                      Wow, this is nice to know. Thank you so much, I am loving learning C# :)

                      Chris Wright

                      OriginalGriffO 1 Reply Last reply
                      0
                      • W wrightyrx7

                        Wow, this is nice to know. Thank you so much, I am loving learning C# :)

                        Chris Wright

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        Phil.o did make a small mistake: highlight your button, and set the "DialogResult" property to "OK" for it to close the form and return DialogResult.OK. The Form.AcceptButton property just tells the form that an ENTER keypress is the equivelant of clicking the button. (Form.CancelButton does the same thing, but with the ESC key.)

                        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        W P 2 Replies Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Phil.o did make a small mistake: highlight your button, and set the "DialogResult" property to "OK" for it to close the form and return DialogResult.OK. The Form.AcceptButton property just tells the form that an ENTER keypress is the equivelant of clicking the button. (Form.CancelButton does the same thing, but with the ESC key.)

                          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                          W Offline
                          W Offline
                          wrightyrx7
                          wrote on last edited by
                          #12

                          Thanks OriginalGriff. Learning is much easier and fun when people like you guys, help people like me out. I was ripping my hair out yesterday trying to figure out how to a value from one form to another. Is the code that Phil.o supplied classed as an event? You mentioned above that - Delegates aren't "the best way" to pass variables back, Events are (which use delegates, but hides the complexities from you).

                          Chris Wright

                          OriginalGriffO 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            Phil.o did make a small mistake: highlight your button, and set the "DialogResult" property to "OK" for it to close the form and return DialogResult.OK. The Form.AcceptButton property just tells the form that an ENTER keypress is the equivelant of clicking the button. (Form.CancelButton does the same thing, but with the ESC key.)

                            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                            P Offline
                            P Offline
                            phil o
                            wrote on last edited by
                            #13

                            You're absolutely right. Thanks for pointing it out.

                            enum HumanBool { Yes, No, Maybe, Perhaps, Probably, ProbablyNot, MostLikely, MostUnlikely, HellYes, HellNo, Wtf }

                            1 Reply Last reply
                            0
                            • W wrightyrx7

                              Thanks OriginalGriff. Learning is much easier and fun when people like you guys, help people like me out. I was ripping my hair out yesterday trying to figure out how to a value from one form to another. Is the code that Phil.o supplied classed as an event? You mentioned above that - Delegates aren't "the best way" to pass variables back, Events are (which use delegates, but hides the complexities from you).

                              Chris Wright

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #14

                              No - that's just using ShowDialog to display Form2, which causes the Form1 code to "freeze", waiting for the new form to close before executing any more code (this is technically called a "Modal Form" but you don't need to worry about it). There is also Form.Show which returns immediately, and that type needs Events to "Know" when data is available for Form1 to process. Follow the link I gave you, and you'll find a download of a source project that shows it all working with Show. If you are going to use Events - and it's a good idea, .NET is built on the buggers - then also have a look here: A Simple Code Snippet to Add an Event[^] - it makes it a lot easier to create them in your own forms and other classes.

                              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                No problem!

                                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                W Offline
                                W Offline
                                wrightyrx7
                                wrote on last edited by
                                #15

                                Is it because I am being stupid or do all three of the examples go from Parent to Child? Transferring information between two forms, Part 2: Child to Parent[^] I could be wrong, maybe its the 'Form Text' that is confusing me.

                                Chris Wright

                                OriginalGriffO 1 Reply Last reply
                                0
                                • W wrightyrx7

                                  Is it because I am being stupid or do all three of the examples go from Parent to Child? Transferring information between two forms, Part 2: Child to Parent[^] I could be wrong, maybe its the 'Form Text' that is confusing me.

                                  Chris Wright

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #16

                                  No, the Child raises the event to say "data available", the Parent handles the event and fetches it using the Child instance and property.

                                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                  1 Reply Last reply
                                  0
                                  • W wrightyrx7

                                    Im trying to figure out Delegates as I have read this is the best way to pass Values from one form to another. I have tried to do this but am getting an error. So to test I am trying to do the following. Form1 has a button which opens Form2 which contains a DatePicker. The user selects a date and clicks OK. I then want the date passed back to a textbox on Form1. This is my code. FORM1 - CODE (The getEffDate in this code is where im getting the error.)

                                    private void TestingButton_Click(object sender, EventArgs e)
                                    {
                                    Form_EffectiveDate effDate = new Form_EffectiveDate();
                                    if (effDate.ShowDialog() == DialogResult.OK)
                                    {
                                    EffDateTextBox.Text = Form_EffectiveDate.getEffDate;
                                    }
                                    }

                                    FORM 2 - Code

                                    namespace Manager_Changes
                                    {
                                    public partial class Form_EffectiveDate : Form
                                    {
                                    public delegate void GetEffDate(object sender);
                                    public GetEffDate getEffDate;

                                        public Form\_EffectiveDate()
                                        {
                                            InitializeComponent();
                                        }
                                    
                                        private void DateOKButton\_Click(object sender, EventArgs e)
                                        {
                                            getEffDate(effDateTimePicker.Value.ToString());
                                            DialogResult = System.Windows.Forms.DialogResult.OK;
                                            this.Close();
                                        }
                                    }
                                    

                                    }

                                    Chris Wright

                                    S Offline
                                    S Offline
                                    Sagar Jaybhay
                                    wrote on last edited by
                                    #17

                                    If you want to pass data from Form2 to Form1. Add static string or DateTime field in Form1 then in Form2 get the value of that datepicker and assign it Form1.staticvariable.

                                    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