How to Ini variables in C#

This is a discussion on How to Ini variables in C# within the C# Programming forums, part of the General Programming Boards category; Hello! Maybe my title name was a bad choice huh? I am new to C# and am using Visual C# ...

  1. #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!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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