C# how to mimic #define X A and #include <defs.h>
#1 Members - Reputation: 106
Posted 14 March 2012 - 07:58 AM
For example
--start defs.h
#define GAME_NAME "REALLY GOOD GAME"
#define MAIN_IP "127.0.0.1"
#define MAIN_PORT 1234
--end defs.h
So, problem 1, can't define anything, problem 2 can't include it if I could. I want every single bit of code part of the code to know some definitions, I'm quite new to C# so please spell it out exactly if you have time to help with this problem.
#2 Moderator* - Reputation: 5362
Posted 14 March 2012 - 08:06 AM
#3 Crossbones+ - Reputation: 1158
Posted 14 March 2012 - 08:08 AM
#4 Members - Reputation: 5802
Posted 14 March 2012 - 08:37 AM
If you want to use "defs.h" as some sort of configuration file, you should probably have an actual configuration file instead, so the program reads in key-value pairs at startup and then different parts of the program can query for whatever configuration they need.
#5 Members - Reputation: 3283
Posted 14 March 2012 - 08:59 AM
Why exactly do you feel that the whole program needs to know about the name of the game or some IP and port? It's probably much better to have non-global variables for this.
If you want to use "defs.h" as some sort of configuration file, you should probably have an actual configuration file instead, so the program reads in key-value pairs at startup and then different parts of the program can query for whatever configuration they need.
This, especially the config file thing.
Take advantage of the language you are using, C# makes using configuration files laughably easy, chances are you already have an app.config xml file that you aren't using. Use it, then accessing it's contents is literally a single line of code.
#6 Members - Reputation: 3329
Posted 14 March 2012 - 09:40 AM
#7 Members - Reputation: 106
Posted 16 March 2012 - 10:16 AM
It looks like what you actually want are global variables (even in C++, it's better to have a global variable than a global #define). C# tried to get people to not use globals, but you can do the same thing by making variables static. While I don't actually really like this page's explanation, I think it's got a simple enough sample: linky.
At first I was just crazy about the lack of include and defines, but through this example I found it really was no big deal and I learned a bit in the process about my new language. Thanks.
#8 Moderator* - Reputation: 5362
Posted 16 March 2012 - 11:42 AM
I'm glad it was useful, but I honestly hope you take to heart what was said about config files. They're a beauty in C#.
It looks like what you actually want are global variables (even in C++, it's better to have a global variable than a global #define). C# tried to get people to not use globals, but you can do the same thing by making variables static. While I don't actually really like this page's explanation, I think it's got a simple enough sample: linky.
At first I was just crazy about the lack of include and defines, but through this example I found it really was no big deal and I learned a bit in the process about my new language. Thanks.






