943,679 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 235679
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 3rd, 2009
0

Re: How to define global variable in C#

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efficacious is offline Offline
12 posts
since May 2009
Aug 4th, 2009
0

Re: How to define global variable in C#

....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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efficacious is offline Offline
12 posts
since May 2009
Aug 4th, 2009
0

Re: How to define global variable in C#

efficacious,

You could just add a static List to a class. For example:
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Aug 4th, 2009
0

Re: How to define global variable in C#

i'm still kinda a noob at C# can u show how i can use that class to make new global variables..ty
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efficacious is offline Offline
12 posts
since May 2009
Aug 4th, 2009
0

Re: How to define global variable in C#

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:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Aug 6th, 2009
0

Re: How to define global variable in C#

can use session variables...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ghuman is offline Offline
1 posts
since Aug 2009
Nov 6th, 2009
0
Re: How to define global variable in C#
Hi Guys
When using a static class, does all its attributes and methods need to be static aswell??
thanks in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
safwaan is offline Offline
1 posts
since Nov 2009
Dec 18th, 2009
0
Re: How to define global variable in C#
worked well. thanks for this info. i'm new in c# and using windows forms because i'm using PB (for win forms) and ASP.Net and VB.net (for web projects). ;-)

again thanks a lot...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saintjanus8 is offline Offline
1 posts
since Dec 2009
Dec 27th, 2009
-1
Re: How to define global variable in C#
i have an idea but its work with VB
look man you will add a module
1-right click on your project
2- add
3- new item
4- module
at the module body you could define whatever you want like....
Module Module1
public ahmed As string
Public x As Integer
Public y As Integer
Public z As Integer = 59
End Module

and you will find those variable at any form you have

plz if any one know what is the module at c# items tell me
thats 100% work at VB
thanx alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fantazia is offline Offline
1 posts
since Dec 2009
Jan 2nd, 2010
0
Re: How to define global variable in C#
dude you need to declare the global variable in program.cs
it would help you to access such variable in every form.
try this out if it wont works i will show you an example by retrieving username and usertype which is your need
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
mith_cool is offline Offline
56 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Windows Service won't auto start
Next Thread in C# Forum Timeline: Disable close button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC