C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-28-2002, 12:12 AM   #1
Registered User
 
Join Date: Nov 2002
Posts: 19
How to Ini variables in C#

Hello! Maybe my title name was a bad choice huh? I am new to C# and am using Visual C# .NET

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   Reply With Quote
Old 11-28-2002, 01:34 AM   #2
the hat of redundancy hat
 
nvoigt's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22