| | |
How to define global variable in C#
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 83
Reputation:
Solved Threads: 18
efficacious,
You could just add a static List to a class. For example:
You could just add a static List to a class. For example:
C# Syntax (Toggle Plain Text)
public class Global { //instance variables static List<Global> CLASSES = new List<Global>(); public static Global this[int n] { get { return CLASSES[n]; } } public Global() { CLASSES.Add(this); } }
•
•
Join Date: Aug 2008
Posts: 83
Reputation:
Solved Threads: 18
•
•
•
•
i'm still kinda a noob at C# can u show how i can use that class to make new global variables..ty
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; public class Global { private static readonly List<Global> CLASSES = new List<Global>(); public static int Count { get { return CLASSES.Count; } } public static Global Get(int index) { return CLASSES[index]; } public int value; public Global() { CLASSES.Add(this); } } public static class Test { static void Main() { new Global(); Global.Get(0).value = 1; Console.WriteLine(Global.Get(0).value); Console.ReadKey(); } }
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: How to CREATE a mySQL database in C# code
- Next Thread: Hi, I dont get what Iv done wrong
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml





