Results 1 to 6 of 6

Thread: Global Constants and Variables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    127

    Global Constants and Variables

    There seems to be a lot of arguments over the use of Global variables in C# - I'm an 'old' programmer, so what I consider 'best practice' - or was 30yrs ago - has changed somewhat... hehe

    I've read a bunch of posts on various forums, but most responses are a bit flippant - not offering a solution, just saying not to use globals - so I'll try to ask here

    The application is a scientific one...I am looking for the 'best practice'

    2 things (related) -

    one - I have a bunch of constants - 200 or so - they are ACTUAL constants, not just programmatic ones - ergo unless the nature of the universe changes, they won't - I'm thinking one of these ideas, just not sure which

    Code:
    class Constants
        {
    
            // Idea #1
            public decimal hDensity { get;} = 1.1209381092831M;
    	//Idea #2
            public static decimal hDensity { get;} = 1.1209381092831M;
            //Idea #3
            public const decimal hDensity { get;} = 1.1209381092831M;
            //Idea #4
            public static decimal hDensity => 1.1209381092831M;
        }
    
    //OR should I use a struct - Idea #5
    
    struct TheConstants
    {
       // definitions
    }


    SECOND - I have a bunch of user settings - not often changed, but.... (both UI related and mathematical stuff)


    I was thinking of using a class or struct also - obviously with get/set this time - likely static.... (??) - and reading those values in from an XML file on launch - and writing to that XML file when needed (I may also include some min/max data in the XML file to limit appropriately the allowable values...

    I know I can make this work.... but is it the 'best' and/or proper way??? and which ideas are 'proper'

    There was a suggestion to use a db (e.g. access) - I really don't like that idea - too much overhead I think for something needed 99.9% of the time ONLY at app launch....

    I appreciate this - I assume there have been similar questions, but the ones I've read so far don't seem to be answered seriously (if I can say that)

    Cheers.... Scott

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Global Constants and Variables

    Static class with proper public constants:

    Code:
    static class Constants
    {
        public const double Pi = 3.14159;
        public const int SpeedOfLight = 300000; // km per sec.
    
    }
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Global Constants and Variables

    Global variables are bad (because one part of the project can accidentally affect another), but Global constants don't have that problem because they cannot change. Global constants are a good idea if they are needed by more than one file in the project.

    The code would be something like:
    Code:
    static class Constants
    {
        public const decimal hDensity = 1.1209381092831M;
        public const int answerToEveryThing = 42;
    }
    ...and the usage would be to simply refer to them (without creating an instance of the class), eg:
    Code:
      myValue = 3 * Constants.hDensity;

    For your user settings, look in to the built-in Settings options (you set the initial values in the project properties). I'm afraid I don't have an example handy.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    127

    Re: Global Constants and Variables

    Perfect..... That was the answer I was looking for...

    My confusion was due to an evolution in terminology since the 'old' days.... and perhaps the fact that we didn't actually have a 'constant' type and as such the term variable applied pretty much to everything.... (long before HHGTTG)

    I'm comfortable using class as well - I understand there are some memory handling differences between class and struct, but it doesn't seem to important in my case

    I'll follow your hint on the other as well -

    Again - thanks.... sometimes it just takes the right words to lift a wack of confusion... simple or not...
    Last edited by ssampson; Aug 9th, 2018 at 09:14 PM.

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: Global Constants and Variables

    enums are a good choice as well. My coworkers are fond of them because you see a name instead of a number
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Global Constants and Variables

    Quote Originally Posted by Lord Orwell View Post
    enums are a good choice as well. My coworkers are fond of them because you see a name instead of a number
    Enums are great for what they're for but it doesn't really sound like they would be appropriate here. An Enum is supposed to represent a valid range of options for a single value. It would not be appropriate to use an Enum for the types of things that constants should be used for, e.g. Math.PI is a constant and could not be appropriately represented using an Enum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width