2

I am new to C# and trying to take what I have learned in PHP / Laravel and transition it over. I am trying to setup a definition list for environmental variables, similar to https://github.com/laravel/laravel/blob/master/.env.example.

I can't figure out how to get the variables to be in scope for me to use other places.

To get started I created a new file URLlist.cs and have this in there:

namespace WindowsFormsApp1
{
    public class URLlist
    {
        public const string getLinks = "https://www.example.com/api/v1/geturls"; 
        public const string postLinks = "https://www.example.com/api/v1/posturls";       
    }
}

I was hoping to be able to use something like URLlist.getLinks, URLlist::getLinks, or URLlist=>getLinks. No matter what I have tried, I can't access those variables from within the Main() scope.

Do I have to define a class for every URL / variable? Seems really repetitive and like something someone would have solved by now, so I thought I would ask.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Alan
  • 2,046
  • 2
  • 20
  • 43
  • 2
    "Do I have to define a class for every URL / variable?" no, but every constant/variable must be within a class, not directly under a namespace, and the class must be either `public` to be used everywhere or `internal` to be used within the same assembly. I would also say that configuration files and settings are much preferred in .NET to constants in code. – D Stanley Jan 04 '18 at 01:35
  • Sorry, I was moving code around describing the problem at the office and removed the class during the discussion with a PHP developer and didn't realize that until you mentioned it. I put it back in. You mention the configuration files / settings, isn't that what I am putting together? If not can you link me an example? – Alan Jan 04 '18 at 01:43
  • 1
    no, configuration files are XML files that are read using standard libraries and can be edited by an adminstrator. When you put constants in code you have to rebuild and redeploy to change settings. You can read more at [MSDN](https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/) or search this and other sites for examples. For what you've posted, however, `URLlist.getLinks` would be the right syntax - something else is wrong. – D Stanley Jan 04 '18 at 01:58
  • `URLLlist.getLinks` (that should be `UrlList.GetLinks` by the way) should be valid. Are you trying to access the constants from another assembly or namespace? Do you have your references and `using` statements in place? – InBetween Jan 04 '18 at 01:59
  • 1
    Might be helpful to review [scope](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#scopes) and [namespace](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/). – Jasen Jan 04 '18 at 02:04
  • @InBetween To prevent any confusion, you must have meant [using directives](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive) (as in `using WindowsFormsApp1;`) rather than [using statements](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement). – Tom Blodget Jan 04 '18 at 03:15
  • https://stackoverflow.com/questions/6867429/cant-read-a-const-in-a-class-instance you may refer to this for your concern, to clear things a bit for you. But the main point here is that, you need to create a read-only property instead of declaring a constant variable – JC Borlagdan Jan 04 '18 at 03:29
  • 1
    Without [MCVE] it is unclear why `URLlist.getLinks` did not work in your case. It is also not very clear what exactly you are trying to figure out - it feels like you simply disagree with the fact that [C# does not have global variables](https://stackoverflow.com/questions/14368129/c-sharp-global-variables). – Alexei Levenkov Jan 04 '18 at 03:46

2 Answers2

0

I do not understand exactly what do you mean here by environment variables, because that may change according to the language.

If you mean to define something generally accessible. Maybe static properties?

public class GeneralSettings
{
    public static string SettingA { get; set; }
    public static string SettingB { get; set; }
}

Or maybe constants (which cannot be changed in runtime):

public class GeneralConstants
{
    public const string SettingA;
    public const string SettingB;
}

if you mean something to change the general behavior of the program (while building, debugging, deploying). You can add it to Visual Studio build settings like this:

enter image description here

This approach is here well documented

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • @InBetween, I did not say that consts are variables. – Mohammed Noureldin Jan 04 '18 at 02:07
  • 1
    I never said you did, I was only pointing out that the correct term is *static fields*, not *static variables*. Anyway, the main idea of my comment was that the OP is already using `const` fields which essentially makes your suggestion of using *static* fields moot. – InBetween Jan 04 '18 at 02:10
  • @InBetween, I made it `properties`, that makes it better. Isn't it correct? – Mohammed Noureldin Jan 04 '18 at 02:12
  • 1
    I was able to do it using `public static string GetLinks = xxx` as you suggested. Then I could call it using `URLlist.GetLinks` Thank you. – Alan Jan 04 '18 at 15:35
  • 1
    @Alan, you are welcome, just for your information: `public static string GetLinks = xxx` is called a static field. Where `public static string GetLinks { get; set; } = xxx` is called a static property. It is recommended when you expose anything to the public to expose it as a property. – Mohammed Noureldin Jan 04 '18 at 15:44
0

If this is all for a single server but many processes, you can define your environmental variables in Windows so all applications can read them.

You can read them in c# via

System.Environment.GetEnvironmentVariable("variable-name")
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40