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. Why property or field unable to give the contents ?

Why property or field unable to give the contents ?

Scheduled Pinned Locked Moved C#
question
8 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.
  • K Offline
    K Offline
    kalaveer
    wrote on last edited by
    #1

    i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:

    C G J P 4 Replies Last reply
    0
    • K kalaveer

      i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:

      C Offline
      C Offline
      chravisankar
      wrote on last edited by
      #2

      instead of initializing st in Form_Load ,initialize it in the constructor.

      "Change is the only constant thing in life Either or survive or get extinct"

      K 1 Reply Last reply
      0
      • K kalaveer

        i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        If you want to access a value that is present in the current instance of the form, you need a reference to that instance. If you create a new instance of the form, you will always get the same value, as it's a newly created and unused form.

        --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

        1 Reply Last reply
        0
        • C chravisankar

          instead of initializing st in Form_Load ,initialize it in the constructor.

          "Change is the only constant thing in life Either or survive or get extinct"

          K Offline
          K Offline
          kalaveer
          wrote on last edited by
          #4

          but if i need to assign that value after occurrance of some event then how could i get the content? :|

          1 Reply Last reply
          0
          • K kalaveer

            i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:

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

            kalaveer wrote:

            private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; }

            kalaveer wrote:

            Form1 f = new Form1(); MessageBox.Show(f.s);

            You have initialised your variable when the Form Loads (Form_LOad event). This event is fired when the form is loaded (ie. displayed). As you have not shown your form your property has not been set Form1 f = new Form1(); f.Show(); // important! MessageBox.Show(f.s);

            --- How to get answers to your questions[^]

            K 1 Reply Last reply
            0
            • K kalaveer

              i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              In your example, the Form_Load event has not been fired. You need to set the value in the constructor based on your logic.

              the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
              Deja View - the feeling that you've seen this post before.

              1 Reply Last reply
              0
              • J J4amieC

                kalaveer wrote:

                private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; }

                kalaveer wrote:

                Form1 f = new Form1(); MessageBox.Show(f.s);

                You have initialised your variable when the Form Loads (Form_LOad event). This event is fired when the form is loaded (ie. displayed). As you have not shown your form your property has not been set Form1 f = new Form1(); f.Show(); // important! MessageBox.Show(f.s);

                --- How to get answers to your questions[^]

                K Offline
                K Offline
                kalaveer
                wrote on last edited by
                #7

                Wow thats working but it displaying the form again how can i avoid this. my first form already visible and with f.show() its displayed again i tried to make invisible but by setting visible property to false not working as f.show is just opposite of hiding na please suggest:|

                G 1 Reply Last reply
                0
                • K kalaveer

                  Wow thats working but it displaying the form again how can i avoid this. my first form already visible and with f.show() its displayed again i tried to make invisible but by setting visible property to false not working as f.show is just opposite of hiding na please suggest:|

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  kalaveer wrote:

                  Wow thats working but it displaying the form again how can i avoid this.

                  As I already explained, you need a reference to the existing form, not creating a new one.

                  --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                  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