Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: (Static) Class-Level Variables

This is definitely a bit of a noob question, but my searches so afar haven't cleared the issue up for me.

A want a particular console app to store several class-level variables. In one case, I want to store a copy of my logging object, which I'll use in various places within the class. In another case, I want to store a simple type, a int value actually, which is only going to be used internally (doesn't need to be a property).

It appears that unless I specify these variables as static, I can't use them in Main() and beyond.

My understanding of static objects is that their values are shared across all instances of an object. Under normal operation, I'd expect their to be only one instance of my app, so this issue isn't a problem - but it has highlighted a lack of understanding on my part of something that is fairly fundamental.

In the case, of my logging object, I could see a case for making it static - sharing a log across multiple instances might be a benefit. However, it might not be the case... In the case of my int, I'd certainly never want this to be shared across instances.

So...

  • Am I misunderstanding the theory behind this?
  • Is there a different way I should be declaring and using my class-level variables?
  • Should I be avoiding using them? I could simply pass values as parameters from function to function, though it seems little a lot for work for no apparent gain.

EDIT: OK, the message is clear - my understanding of statics was largely correct, but the problem was one of structure and approach. Thanks for your replies.

like image 320
CJM Avatar asked Oct 01 '10 13:10

CJM


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Just encapsulate your application in another class, which you create and execute on the Main method:

class MyApp {
  private MyLog lol = new MyLog(); 
  private int myInt = 0;

  public void Execute() {
    // ...
  }
}

class Program {
  public static void Main() {
    new MyApp().Execute();
  }
}

You can still make the log field static if you want.

like image 63
Jordão Avatar answered Sep 30 '22 01:09

Jordão


You should be creating a class outside of your Main function, and then creating an instance of that class from within Main.

EG

class MyConsoleApp
{
    public static void Main()
    {
        MyClass mc = new MyClass();
    }
}

Class MyClass
{
   private MyLog lol as new MyLog();
   private int myInt = 0;
}
like image 20
DJ Quimby Avatar answered Sep 30 '22 01:09

DJ Quimby