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. How to create global variable

How to create global variable

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

    Hi all, In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below: Class GetSet: private String _name; public String name { get{return _name;} set{_name = value;} } FormA: public partial class FrmA : Form { GetSet gs; public FrmMain() { gs = new GetSet(); gs.name = "cocoon"; } } FormB: public partial class FrmB : Form { GetSet gs; public FrmMain() { console.write(gs.name); } } Can anyone guide me to slove this out. thanks in advance regards cocoon

    V C C S 4 Replies Last reply
    0
    • C cocoonwls

      Hi all, In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below: Class GetSet: private String _name; public String name { get{return _name;} set{_name = value;} } FormA: public partial class FrmA : Form { GetSet gs; public FrmMain() { gs = new GetSet(); gs.name = "cocoon"; } } FormB: public partial class FrmB : Form { GetSet gs; public FrmMain() { console.write(gs.name); } } Can anyone guide me to slove this out. thanks in advance regards cocoon

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      You have to rethink your design.

      cocoonwls wrote:

      How to create global variable

      You can't. However, you can create a static class which is quite similar to what you are asking for. There are plenty of examples on Google.

      Cheers, Vikram.


      The hands that help are holier than the lips that pray.

      1 Reply Last reply
      0
      • C cocoonwls

        Hi all, In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below: Class GetSet: private String _name; public String name { get{return _name;} set{_name = value;} } FormA: public partial class FrmA : Form { GetSet gs; public FrmMain() { gs = new GetSet(); gs.name = "cocoon"; } } FormB: public partial class FrmB : Form { GetSet gs; public FrmMain() { console.write(gs.name); } } Can anyone guide me to slove this out. thanks in advance regards cocoon

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        The way to do this, is to use delegates, or create a class with static properties. If you thought that FrmB would do anything but crash, or had any chance of seeing hte value 'cocoon', you need to do some serious reading on OO, because you've misunderstood quite a bit.

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        1 Reply Last reply
        0
        • C cocoonwls

          Hi all, In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below: Class GetSet: private String _name; public String name { get{return _name;} set{_name = value;} } FormA: public partial class FrmA : Form { GetSet gs; public FrmMain() { gs = new GetSet(); gs.name = "cocoon"; } } FormB: public partial class FrmB : Form { GetSet gs; public FrmMain() { console.write(gs.name); } } Can anyone guide me to slove this out. thanks in advance regards cocoon

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

          Hi Vikram and Christian, Thanks for your reply. I have read through the static class and it useful to me. Then, Christian Graus, can you please explan a bit more to me why would you said i am misunderstood OO concept? Thanks in advance. ;) regards cocoon

          1 Reply Last reply
          0
          • C cocoonwls

            Hi all, In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below: Class GetSet: private String _name; public String name { get{return _name;} set{_name = value;} } FormA: public partial class FrmA : Form { GetSet gs; public FrmMain() { gs = new GetSet(); gs.name = "cocoon"; } } FormB: public partial class FrmB : Form { GetSet gs; public FrmMain() { console.write(gs.name); } } Can anyone guide me to slove this out. thanks in advance regards cocoon

            S Offline
            S Offline
            sarvesh upadhyay
            wrote on last edited by
            #5

            You can use singleton class object. It will help in state maintainance in the application.

            Sarvesh Upadhyay Senior Software Engineer Birlasoft India Ltd. Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application

            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