Click here to Skip to main content
15,884,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is possible to declare global VAR in c# .


i have a class file called "Class1.cs". class1 contains two methods. i want to use single var object in both class.

Example;

C#
class1
{
   var i=0;
   void add()
  {
     console.write(i);
  }
  void multiple()
  {
    console.write(i);
  }

}
Posted
Updated 20-Jun-20 17:58pm

As this MSDN article shows Implicitly Typed Local Variables (C# Programming Guide)[^]

Quote:
The var keyword may be used in the following contexts:


On local variables (variables declared at method scope) as shown in the previous example.


So to answer your question using the var declaration at class level wont work.
 
Share this answer
 
v2
 
Share this answer
 
Comments
Murugesan Solaiyappan 1-Mar-13 3:55am    
i think you don't understand what i expect. this is not my expected answer. i am not mean about C# variable declaration. here i posted about "VAR" object .
Hi,
In non OO world, mainly in C it is used as #defined.

But in OO world it is not best practice to use variable as publicly visible member, for many reason.
However, if you want to make a variable public then you may want to use them as static constant.
C#
public static class GlobalVariables
{
   public static const int InputNumber = 25;
}

public class Class1
{
   var i = GlobalVariables.InputNumber;
}
 
public class Class2
{
   var i = GlobalVariables.InputNumber;
}


Unless the InputNumber is never going to change, I wouldn't use it as constant.

It would be far better if you derive the variable as a property from a derived class, either as Abstract or Interface.

Regadrs
Jegan
 
Share this answer
 
Comments
Per Söderlund 3-Mar-13 17:04pm    
What are you doing here and why?
I fail to see how this is practical and when to use it.

Also, why is it not best practice to use a public variable?
Oh and another thing, why use a static const int instead of const int?
Jegan Thiyagesan 3-Mar-13 17:50pm    
I am not sure what do you mean by "What am I doing here and Why?"

You may use global variable if the application has some default values that get populated in multiple of forms, if the use changes the default value, the new value will be stored in the data store but if the user decided to revert back to default value, the values from the hard coded global variable will be applied.

In OO the variable are never exposed as public they are always private to the class it is belong to, it is only exposed either as Properties or Geters and Setters.
Here is an article about what a class should do http://pragprog.com/articles/tell-dont-ask .

const int = this variable will receive a value only during its construction,
static const int = there will be only 1 such class-wide data member.

I hope this helps.

Jegan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900