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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to make a Class object global??

How to make a Class object global??

Scheduled Pinned Locked Moved C#
tutorialquestion
6 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.
  • S Offline
    S Offline
    suguimoto
    wrote on last edited by
    #1

    Hi! I would like to know if it´s possible to make a class object global. Just like if I declared once, and a lot of classes can work with it, keeping the same values. Because if I instantiate an object for every class... the values go blank =/ (Class obj = new Class()) Thnx

    P B G 3 Replies Last reply
    0
    • S suguimoto

      Hi! I would like to know if it´s possible to make a class object global. Just like if I declared once, and a lot of classes can work with it, keeping the same values. Because if I instantiate an object for every class... the values go blank =/ (Class obj = new Class()) Thnx

      P Offline
      P Offline
      psamy
      wrote on last edited by
      #2

      Hi, make function and property as static so that you can call from anywhere without creating instance -- modified at 6:39 Friday 7th July, 2006

      1 Reply Last reply
      0
      • S suguimoto

        Hi! I would like to know if it´s possible to make a class object global. Just like if I declared once, and a lot of classes can work with it, keeping the same values. Because if I instantiate an object for every class... the values go blank =/ (Class obj = new Class()) Thnx

        B Offline
        B Offline
        Brendan Vogt
        wrote on last edited by
        #3

        Yeah something like: public static decimal ComputeInterest() { // Code here }

        1 Reply Last reply
        0
        • S suguimoto

          Hi! I would like to know if it´s possible to make a class object global. Just like if I declared once, and a lot of classes can work with it, keeping the same values. Because if I instantiate an object for every class... the values go blank =/ (Class obj = new Class()) Thnx

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

          If you actually need an object instead of just static methods in a class, you can make a singleton:

          public class Singleton {

          private Singleton instance;

          private Singleton() {}

          public Singletton GetInstance() {
          if (this.instance == null) {
          this.instance = new Singleton();
          }
          return this.instance;
          }

          }

          --- b { font-weight: normal; }>

          E 1 Reply Last reply
          0
          • G Guffa

            If you actually need an object instead of just static methods in a class, you can make a singleton:

            public class Singleton {

            private Singleton instance;

            private Singleton() {}

            public Singletton GetInstance() {
            if (this.instance == null) {
            this.instance = new Singleton();
            }
            return this.instance;
            }

            }

            --- b { font-weight: normal; }>

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            private static Singleton instance; public static Singletton GetInstance(); might though. Also, I read once, that private static Singleton instance = new Singleton(); would be gauranteed to be accessed in order and is not subject to threading issues. Whereas this.instance == null can cause threading problems. A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

            G 1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              private static Singleton instance; public static Singletton GetInstance(); might though. Also, I read once, that private static Singleton instance = new Singleton(); would be gauranteed to be accessed in order and is not subject to threading issues. Whereas this.instance == null can cause threading problems. A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

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

              Ennis Ray Lynch, Jr. wrote:

              private static Singleton instance; public static Singletton GetInstance(); might though.

              Yes, correct.

              Ennis Ray Lynch, Jr. wrote:

              Whereas this.instance == null can cause threading problems.

              Yes, to make it thread safe you need locking. --- b { font-weight: normal; }

              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