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. A True Global Class?

A True Global Class?

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 2 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.
  • R Offline
    R Offline
    redfish34
    wrote on last edited by
    #1

    In VB6 i could instantiate a class in a module as public and this would become global to the entire application. This was useful for a settings class where the settings were loaded from a file when the app started, then classes would share these settings and modify them, then the settings file would updated when the app closed. Is there a way to do similar in C#? Where do i need to put the settings class so that it is global to all other classes in a namespace?

    C 1 Reply Last reply
    0
    • R redfish34

      In VB6 i could instantiate a class in a module as public and this would become global to the entire application. This was useful for a settings class where the settings were loaded from a file when the app started, then classes would share these settings and modify them, then the settings file would updated when the app closed. Is there a way to do similar in C#? Where do i need to put the settings class so that it is global to all other classes in a namespace?

      C Offline
      C Offline
      Craig G Fraser
      wrote on last edited by
      #2

      I use a class to hold all my "global" settings which i load on application startup. These values are accessable from anywhere in the app. Heres how you do it: 1) Create a class which is public 2) Add public static variables where you will store your settings. public class Globals { public static bool bBoolvalue; public static string sStringValue; public Globals() { } } 3) Then when you app starts up - just assign values to these variables eg: Globals.sStringValue = "This is a string"; 4) This variable will now contain the assigned value and because it is public static you will be able to reference it from anywhere in your app eg: string sTest = Globals.sStringValue; Hope this helps, Cheers Craig ** I'd rather try and fail than fail to try **

      R 1 Reply Last reply
      0
      • C Craig G Fraser

        I use a class to hold all my "global" settings which i load on application startup. These values are accessable from anywhere in the app. Heres how you do it: 1) Create a class which is public 2) Add public static variables where you will store your settings. public class Globals { public static bool bBoolvalue; public static string sStringValue; public Globals() { } } 3) Then when you app starts up - just assign values to these variables eg: Globals.sStringValue = "This is a string"; 4) This variable will now contain the assigned value and because it is public static you will be able to reference it from anywhere in your app eg: string sTest = Globals.sStringValue; Hope this helps, Cheers Craig ** I'd rather try and fail than fail to try **

        R Offline
        R Offline
        redfish34
        wrote on last edited by
        #3

        Craig, Thanks for the advice. Coming from VB6, i did not use static all that much. I took your static idea one step further and used it with properties and methods so i could duplicate what i did in VB6. It works!

        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