![]() |
| | #1 |
| Registered User Join Date: Nov 2002
Posts: 19
| How to Ini variables in C# I need to know where in my code that I can Ini variables so that they can be used anywhere in my program for example. Say I wanted to use DirectSound Ds; more than once. Where do I place it? Thanks!
__________________ May GOD Bless, Strengthen, Guide, and Protect you Always! |
| FwyWice is offline | |
| | #2 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,770
| You cannot have variables outside of a class like in C/C++. However, if you have static member of a class, you can access them from anywhere you can access the class type. Example: You have one class that contains the static main function. If this is your form class as VS puts it as default I suggest you put the main function in a class of it's own. Call it "Global" or something similar. Now for each global variable you need create a public static member. Take an integer for example: class Global { public static int Test = 7; //main function here } Now you can access this int anywhere: Global.Test = 8; if( Global.Test != 9 ) ... Please note that if a class contains only static members, as this probably will, it is good practice to have an explicit private contructor, so noone using this class can create an instance of it.
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| basic question about global variables | radeberger | C++ Programming | 0 | 04-06-2009 12:54 AM |
| Is there a logical way of figuring out where variables are stored? | mr_coffee | C Programming | 15 | 09-30-2008 02:12 AM |
| esbo's data sharing example | esbo | C Programming | 49 | 01-08-2008 11:07 PM |
| Declaring an variable number of variables | Decrypt | C++ Programming | 8 | 02-27-2005 04:46 PM |
| functions to return 2 variables? | tim | C Programming | 5 | 02-18-2002 02:39 PM |