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. I don't understand why I can't access a function from my Form instance from another class in the same namespace

I don't understand why I can't access a function from my Form instance from another class in the same namespace

Scheduled Pinned Locked Moved C#
helpquestioncsharp
9 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.
  • H Offline
    H Offline
    haz13
    wrote on last edited by
    #1

    I am new to c# and oop. I seem to be missing something here. I don't understand why I can't access a function from my Form instance (Form1) from another class in the same namespace. Why does it not recognise the Form1 instance? Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class. Could you please insert the correct code where necessary to make it work so I can fully understand what I need to do. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); } public void SetFormData() { //here is where I will set the textbox properties textBox1.Text = "Hello"; } } } -------------- namespace RouletteV1 { public static class NumberAttributes { public void Load(short Number) { // load the number data into global properties here NumberProcessing.UpdateNumArray(); } } --------------- namespace RouletteV1 { public static class NumberProcessing { public void UpdateNumArray() { // Here I want to call the SetFormData()function in Form1 but it won't recognise it - why??? Form1.SetFormData(); } } } Thanks in advance for your help Haz

    N 1 Reply Last reply
    0
    • H haz13

      I am new to c# and oop. I seem to be missing something here. I don't understand why I can't access a function from my Form instance (Form1) from another class in the same namespace. Why does it not recognise the Form1 instance? Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class. Could you please insert the correct code where necessary to make it work so I can fully understand what I need to do. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); } public void SetFormData() { //here is where I will set the textbox properties textBox1.Text = "Hello"; } } } -------------- namespace RouletteV1 { public static class NumberAttributes { public void Load(short Number) { // load the number data into global properties here NumberProcessing.UpdateNumArray(); } } --------------- namespace RouletteV1 { public static class NumberProcessing { public void UpdateNumArray() { // Here I want to call the SetFormData()function in Form1 but it won't recognise it - why??? Form1.SetFormData(); } } } Thanks in advance for your help Haz

      N Offline
      N Offline
      NaNg15241
      wrote on last edited by
      #2

      Becouse you are trying to access a non static class from a static class... it's not possible in C#. I don't know how can I help you now, maybe someone else can.

      H B 2 Replies Last reply
      0
      • N NaNg15241

        Becouse you are trying to access a non static class from a static class... it's not possible in C#. I don't know how can I help you now, maybe someone else can.

        H Offline
        H Offline
        haz13
        wrote on last edited by
        #3

        Can anyone else help me with this?

        Haz

        RaviBeeR 1 Reply Last reply
        0
        • H haz13

          Can anyone else help me with this?

          Haz

          RaviBeeR Offline
          RaviBeeR Offline
          RaviBee
          wrote on last edited by
          #4

          The method SetFormData() can only be performed on an instance of a Form1 object. One way to achieve this is to pass UpdateNumArray() a reference to a Form1 object, as in:

          public void UpdateNumArray
          (Form1 theForm)
          {
          theForm.SetFormData();
          }

          /ravi

          My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

          H 1 Reply Last reply
          0
          • RaviBeeR RaviBee

            The method SetFormData() can only be performed on an instance of a Form1 object. One way to achieve this is to pass UpdateNumArray() a reference to a Form1 object, as in:

            public void UpdateNumArray
            (Form1 theForm)
            {
            theForm.SetFormData();
            }

            /ravi

            My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            H Offline
            H Offline
            haz13
            wrote on last edited by
            #5

            Thanks for that, everything has just clicked!!!! I understand where I was going wrong!! Fundamentals of oop :)

            Haz

            RaviBeeR 1 Reply Last reply
            0
            • N NaNg15241

              Becouse you are trying to access a non static class from a static class... it's not possible in C#. I don't know how can I help you now, maybe someone else can.

              B Offline
              B Offline
              BoneSoft
              wrote on last edited by
              #6

              Wrong. The problem is he's trying to access the non-static method on the Form1 class instead of an instance of Form1. The class trying to access an instance of Form1 can only get a reference to the instance of Form1 if it creates the form instance, or the form gives the using class an instance of itself. The following will work, but it's not a very wise design decision to allow processing classes to has instances of your Form. Better to have you working classes just do work with data and return values that your UI can use.

              namespace RouletteV1 {
              public partial class Form1 : Form {
              public Form1() {
              InitializeComponent();
              }

                  private void btn1\_Click(object sender, EventArgs e) {
                      NumberAttributes.Load(this, 1);
                  }
              
                  public void SetFormData() {
                      textBox1.Text = "Hello";
                  }
              }
              
              public static class NumberAttributes {
                  public void Load(Form1 form, short Number) {
                      NumberProcessing.UpdateNumArray(form);
                  }
              }
              
              public static class NumberProcessing {
                  public void UpdateNumArray(Form1 form) {
                      form.SetFormData();
                  }
              }
              

              }


              Try code model generation tools at BoneSoft.com.

              -- modified at 11:46 Monday 24th July, 2006 Whoever voted this message down can kiss my hairy white ass, unless they'd like to actually post something to refute it.

              H 1 Reply Last reply
              0
              • H haz13

                Thanks for that, everything has just clicked!!!! I understand where I was going wrong!! Fundamentals of oop :)

                Haz

                RaviBeeR Offline
                RaviBeeR Offline
                RaviBee
                wrote on last edited by
                #7

                Glad you're up and running! /ravi

                My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • B BoneSoft

                  Wrong. The problem is he's trying to access the non-static method on the Form1 class instead of an instance of Form1. The class trying to access an instance of Form1 can only get a reference to the instance of Form1 if it creates the form instance, or the form gives the using class an instance of itself. The following will work, but it's not a very wise design decision to allow processing classes to has instances of your Form. Better to have you working classes just do work with data and return values that your UI can use.

                  namespace RouletteV1 {
                  public partial class Form1 : Form {
                  public Form1() {
                  InitializeComponent();
                  }

                      private void btn1\_Click(object sender, EventArgs e) {
                          NumberAttributes.Load(this, 1);
                      }
                  
                      public void SetFormData() {
                          textBox1.Text = "Hello";
                      }
                  }
                  
                  public static class NumberAttributes {
                      public void Load(Form1 form, short Number) {
                          NumberProcessing.UpdateNumArray(form);
                      }
                  }
                  
                  public static class NumberProcessing {
                      public void UpdateNumArray(Form1 form) {
                          form.SetFormData();
                      }
                  }
                  

                  }


                  Try code model generation tools at BoneSoft.com.

                  -- modified at 11:46 Monday 24th July, 2006 Whoever voted this message down can kiss my hairy white ass, unless they'd like to actually post something to refute it.

                  H Offline
                  H Offline
                  haz13
                  wrote on last edited by
                  #8

                  Thanks a lot for that, you were exactly right! I have slightly altered my code to cut down on the form reference passing. However when I try compling it it throws up an error - Compiler Error CS0120 - Error Message "An object reference is required for the nonstatic field, method, or property 'member' " and refers to line NumberProcessing.UpdateNumArray(this); I am guessing there is a problem with the "this" value? Any suggestions what is wrong??? Code is below. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); NumberProcessing.UpdateNumArray(this); } public void SetFormData() { tboxNumArray.Text = "Hello"; } public class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } } } Thanks in advance for your help :)

                  Haz

                  H 1 Reply Last reply
                  0
                  • H haz13

                    Thanks a lot for that, you were exactly right! I have slightly altered my code to cut down on the form reference passing. However when I try compling it it throws up an error - Compiler Error CS0120 - Error Message "An object reference is required for the nonstatic field, method, or property 'member' " and refers to line NumberProcessing.UpdateNumArray(this); I am guessing there is a problem with the "this" value? Any suggestions what is wrong??? Code is below. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); NumberProcessing.UpdateNumArray(this); } public void SetFormData() { tboxNumArray.Text = "Hello"; } public class NumberProcessing { public void UpdateNumArray(Form1 form) { form.SetFormData(); } } } Thanks in advance for your help :)

                    Haz

                    H Offline
                    H Offline
                    haz13
                    wrote on last edited by
                    #9

                    Forget that I had missed off the static keyword for a method in one of the static classes. Only just realised that all static classes should have static members. Thanks again for your help :)

                    Haz

                    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