Link to home
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

What are the differences when assigning global variables in C#?

What is the difference between defining a value for a parameter? I assume setting it as const assigns it a value and its value cannot be changed. Assigning it as static assigns it a value that can be changed. But what does the third method allow that the others do not. Why define it this way? I am still learning C# and trying to adopt best practices. In this case I have a section of code that will instantiate these as global variables to be used throughout the entire application.

public const DateTime ApplyDate = Convert.ToDateTime("1900-01-01 00:00:00.000");

public static DateTime ApplyDate = Convert.ToDateTime("1900-01-01 00:00:00.000");

static public DateTime ApplyDate
    get { return Convert.ToDateTime("1900-01-01 00:00:00.000"); }
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

On the third case you are making the value read only (because there is no setter in property). For all practical purposes it behaves like the first case.
Avatar of AndyAinscow
Flag of Switzerland image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
The first won't even compile. A const is a constant.
I would prefer the last, but your first could be adopted like this (no Convert is needed):

public readonly DateTime ApplyDate = new DateTime(1900, 1, 1);

Open in new window

Avatar of rwheeler23


As long as this will allow the changing of its value then I am good.
static public DateTime ApplyDate
    get { return Convert.ToDateTime("1900-01-01 00:00:00.000"); }

Speaking of global variables I have seen this comment before. What about having a variable that contains the connection string. That value never changes.
Also, in my application I have 12 methods that do various procedures. All twelve of these methods have a call to another method. I want to set up the call to that method that contains the same set of parameters. Not all parameters are needed in each method so some of these parameters are set to blank or zero. That is why I thought defining them once as a global variable and setting there values before calling the  method would be the correct way to go.
You could set them in your App/Config Settings, like you would do for a connection string.

It's a question of preferences. Some don't like const and readonly variables. 
As to why global variables are not good please read the final section of my first comment.  If you don't understand then say what isn't clear.
Not that I disagree about the concept, but how would you modify a readonly variable?
Ignore my previous comment. I am dealing with developing an application built around an ERP solution. The structure of these forms is very similar and what changes are the tables. So if I want to find where I reference a variable I simply do a search and/or debug. Finding where a global variable is assigned a value is rather simple. What started this question were a handful  of indicator fields. Some screens use them and others do not. I like to have all my parameters lined up in neat little rows all in one place. This is a mental affliction I have but I do see your point about global parameters.
Assume your valid date has to be later than 1st January 2010 and before today.  Being able to modify it directly means you need the same code to check in multiple places.  Having a private variable and a public setter means you only need to check the validity in the setter.  (Not to mention when the conditions of validity change with the work that generates)
Thanks for your tips.