Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# global variables

Posted on 2005-03-14
8
Medium Priority
?
302 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

564 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