?
Solved

C# global variables

Posted on 2005-03-14
8
Medium Priority
?
296 Views
Last Modified: 2010-04-16
How do I define global variables, ones that can be modified by multiple functions within a class?
0
Comment
Question by:arichexe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:jaynus
ID: 13539331
Do you mean multiple classes within an assembly? If its within the class, just define a class object..if you want it within teh assembly...declare static variables/methods inside a class

e.g.

internal class MyStaticStuff {
     public static string MyStaticString = "blah!";
     public static XmlDocument MyStaticObject = new XmlDocument();

     public static string MyStaticFunction(string Heh) {
          return(Heh);
     }
}

Make sense? The static attribute creates instances of the properties/methods upon runtime, rather than requiring an instance of the object.
0
 

Author Comment

by:arichexe
ID: 13539601
It's within the class, so how would I define a class object?
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13539620
public class MyClass {
     string variableone;
     string variabletwo;

     public void MyFunction() {

     }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:PoeticAudio
ID: 13539623
BAH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Never use global variables! use properties instead! People! what happened to your oop manners? =o)


public class whatever

0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 13539673
OOPS!!!

public class whatever
{
        private string privateVar;

        public whatever(){privateVar = "yahoo!";} //constructor

        public string Blah //notice different case
        {
               get
               {
                      return blah;
                }
               set
               {
                      blah = value;
               }
         }
}
               

public class AnotherObject
{
          public AnotherObject(){} //constructor
         
          private string GetBlahFromWhatever()
          {
                whatever obj = new whatever()
                return obj.Blah; //returns "yahoo!"
          }
}
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 13539683
oops, it should be private string blah not private string privateVar... damn I can't think today!
0
 

Author Comment

by:arichexe
ID: 13540173
Jaynus, I'm getting "An object reference is required for the nonstatic field, method, or property MyNameSpace.MyClass.MyCount."  What's causing this?

class MyClass {
     int MyCount = 0;

     public static void Main() {
        MyCount = MyCount + 1
        MySubroutine()
     }

     public static void MySubroutine() {
        ConsoleWriteLine(MyCount);
    }
}
0
 
LVL 10

Accepted Solution

by:
jonvaughan earned 200 total points
ID: 13540419
IF you want to hold a global variable / property for all object within that class then you just use a static property / variable.

public static int MyCount.

you can then access it using MyNameSpace.MyClass.MyCount

Jon
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question