How to define global variable in C#

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 11
Reputation: efficacious is an unknown quantity at this point 
Solved Threads: 0
efficacious efficacious is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #11
Aug 3rd, 2009
worked for me aswell but, why does it need to be static? If it wasn't static could we create new instances of a globalVar and hense store multiple variables? I shall be attempting this
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: efficacious is an unknown quantity at this point 
Solved Threads: 0
efficacious efficacious is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #12
Aug 4th, 2009
....I attempted it and failed .. any1 know of a way to do this on a multiple scale? Obviously w/o having to create a whole class several times.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: How to define global variable in C#

 
0
  #13
Aug 4th, 2009
efficacious,

You could just add a static List to a class. For example:
  1. public class Global
  2. {
  3. //instance variables
  4.  
  5. static List<Global> CLASSES = new List<Global>();
  6. public static Global this[int n]
  7. {
  8. get { return CLASSES[n]; }
  9. }
  10. public Global()
  11. {
  12. CLASSES.Add(this);
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: efficacious is an unknown quantity at this point 
Solved Threads: 0
efficacious efficacious is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #14
Aug 4th, 2009
i'm still kinda a noob at C# can u show how i can use that class to make new global variables..ty
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: How to define global variable in C#

 
0
  #15
Aug 4th, 2009
Originally Posted by efficacious View Post
i'm still kinda a noob at C# can u show how i can use that class to make new global variables..ty
Take a look at this:
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Global
  5. {
  6. private static readonly List<Global> CLASSES = new List<Global>();
  7. public static int Count
  8. { get { return CLASSES.Count; } }
  9. public static Global Get(int index)
  10. { return CLASSES[index]; }
  11.  
  12. public int value;
  13.  
  14. public Global()
  15. { CLASSES.Add(this); }
  16. }
  17.  
  18. public static class Test
  19. {
  20. static void Main()
  21. {
  22. new Global();
  23. Global.Get(0).value = 1;
  24. Console.WriteLine(Global.Get(0).value);
  25. Console.ReadKey();
  26. }
  27. }
It's not a very sophisticated example, but hopefully you can learn from it. If you have any more questions, you can feel free to PM me.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: ghuman is an unknown quantity at this point 
Solved Threads: 0
ghuman ghuman is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #16
Aug 6th, 2009
can use session variables...
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: safwaan is an unknown quantity at this point 
Solved Threads: 0
safwaan safwaan is offline Offline
Newbie Poster
 
0
  #17
19 Days Ago
Hi Guys
When using a static class, does all its attributes and methods need to be static aswell??
thanks in advance
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC