A very beginer question
-
Hello..I'm trying to make my first console app in c#. I've just read a very good book and some articles. So, I'm sure many of my fistr questions may seem very dumb for most of you...please don't be so hard ;o) Q: I sow that to declare a constant value, it should be declared indide a class. So...what if I want to declare a constant value with scope in my whole app?..should I declare a big class envolving all the code?. Is this a good practice valid even for console and windows apps? Thanks
-
Hello..I'm trying to make my first console app in c#. I've just read a very good book and some articles. So, I'm sure many of my fistr questions may seem very dumb for most of you...please don't be so hard ;o) Q: I sow that to declare a constant value, it should be declared indide a class. So...what if I want to declare a constant value with scope in my whole app?..should I declare a big class envolving all the code?. Is this a good practice valid even for console and windows apps? Thanks
You could do a class like that:
public class Constants
{
static const int MIN_SOMETHING = 5;
}You can then access them in your whole project like that:
int test = Constants.MIN_SOMETHING;
This is okay for the most important global constants/variables you might need. But don't populate a class just full of static methods to act like an old C program ;) regards -
Hello..I'm trying to make my first console app in c#. I've just read a very good book and some articles. So, I'm sure many of my fistr questions may seem very dumb for most of you...please don't be so hard ;o) Q: I sow that to declare a constant value, it should be declared indide a class. So...what if I want to declare a constant value with scope in my whole app?..should I declare a big class envolving all the code?. Is this a good practice valid even for console and windows apps? Thanks
Perhaps an enum is what you're looking for?
public enum Priorities
{
High = 10,
Medium = 5,
Low = 1
}...
// When you need to use it:
int priority = (int)Priorities.High;Typically, you don't deal with "globals" per se; everything is contained in a class. Now, if you truely do need a 1 instance class with values in those, you can look into using a static class. But there are only rare occassions for this; whenever I see lots of static classes, especially those that share state between other classes, I immediately think "bad design".
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Perhaps an enum is what you're looking for?
public enum Priorities
{
High = 10,
Medium = 5,
Low = 1
}...
// When you need to use it:
int priority = (int)Priorities.High;Typically, you don't deal with "globals" per se; everything is contained in a class. Now, if you truely do need a 1 instance class with values in those, you can look into using a static class. But there are only rare occassions for this; whenever I see lots of static classes, especially those that share state between other classes, I immediately think "bad design".
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango