is it possible (in C#) to have different declarations of a static variable?
e.g.
I have a "static bool test = true".
Now I want a "static int interval" but it should depend on the test.
if (test)
{
static int interval = 1;
}
else
{
static int interval = 5;
}
because all variables aren't in methods and directly declared in a class I cant use any of this as it is...
thanks in advance!
There are two pretty similar ways to achieve that:
use the ternary within your assignement:
class MyClass
{
static bool test = true;
static int interval = (test ? 1 : 5)
}
use a static constructor:
class MyClass
{
static bool test = true;
static int interval;
static MyClass()
{
if(test) interval = 1;
else interval = 5;
}
}
Related
Thanks to the kind folks who answered my previous question from a few days ago, I now know how to pass arguments by reference:
static void Main()
{
int i = 0;
Add(ref i, 100);
// now i == 100
}
static void Add(ref int arg, int increment)
{
arg += increment;
}
But is there a way for me not to just pass i by reference, but actually store its location in another variable? By that I mean use i like I did in my example; affecting the original instance, but in a way that's permanently linked and not leaving scope.
I vaguely know that I could use a pointer to determine the location in unsafe context but I was wondering if I could do this without any of that, or if it is just recommended to use the unsafe method.
If you are using C# 7 you can use ref local and ref return to store an updateable reference to any field.
In this example I change the private field _privateField from 0 to 100 from outside Foo, the class in which it is defined, by returning it as a ref int and updating it by reference.
class Foo
{
private int _privateField = 0;
public ref int GetReference()
{
return ref _privateField;
}
public override string ToString()
{
return _privateField.ToString();
}
}
public class Program
{
public static void Main()
{
var foo = new Foo();
var referenceToPrivateField = foo.GetReference();
referenceToPrivateField = 100;
Console.WriteLine(foo);
}
}
Prior to that, you'd have to store the value in a field contained in an object, and pass around a reference to the object instead.
In this example I change the value from 0 to 100 from outside Foo, even though it is stored (indirectly) in a field that is private inside the Foo instance.
class ValueTypeReference<T> where T : struct
{
public T Value { get; set; }
}
class Foo
{
private ValueTypeReference<int> _privateField = new ValueTypeReference<int>{ Value = 0 };
public ValueTypeReference<int> GetReference()
{
return _privateField;
}
public override string ToString()
{
return _privateField.Value.ToString();
}
}
public class Program
{
public static void Main()
{
var foo = new Foo();
var referenceToPrivateField = foo.GetReference();
referenceToPrivateField.Value = 100;
Console.WriteLine(foo);
}
}
Output:
100
Well, if I udnerstood you correctly, you want the variable to have global scope, which can be achieved by putting variable as class field/property:
class Program
{
private static int _i;
static void Main()
{
_i = 0;
Add(100);
// now _i == 100
}
static void Add(int increment)
{
_i += 100;
}
}
I'm trying to compare an object with an int value such as
if (myObject - 5 == 0)
doSomething();
my class could look something like this: (most setters/getters removed, so don't mind that all variables are private)
public class SomeClass
{
public string name;
private int minValue;
private int maxValue;
private int currValue;
public int getCurrentValue()
{
return currValue;
}
}
What I'm trying to achieve is something like this:
someClassInstance - 5;
to be equal
someClassInstance.getCurrentValue() - 5;
Can I make an override for the object to act as an int (it's own variable) opposed to just being an object?
May be operator is the case?
public class SomeClass {
...
public static int operator -(SomeClass left, int right) {
if (Object.ReferenceEquals(null, left))
throw new ArgumentNullException("left");
return left.getCurrentValue() - right;
}
}
...
SomeClass someClassInstance = new SomeClass(...);
int result = someClassInstance - 5;
Another possibility (based on implicit operator) is to convert SomeClass implicitly to int whenever required:
public class SomeClass {
...
// Whenever int is requiered, but SomeClass exists make a conversion
public static implicit operator int(SomeClass value) {
if (Object.ReferenceEquals(null, value))
throw new ArgumentNullException("value");
return value.getCurrentValue();
}
}
...
SomeClass someClassInstance = new SomeClass(...);
int result = someClassInstance - 5;
Actually you would be much better off overriding operator int, that way you can do far more calculations with less overloads:
using System;
namespace Demo
{
public class SomeClass
{
public string name;
private int minValue;
private int maxValue;
public int currValue;
public int getCurrentValue()
{
return currValue;
}
public static implicit operator int(SomeClass value)
{
if (value == null)
throw new ArgumentNullException("value");
return value.currValue;
}
}
internal class Program
{
private void run()
{
var test = new SomeClass {currValue = 5};
if (test - 5 == 0)
Console.WriteLine("It worked");
if (test + 5 == 10)
Console.WriteLine("This also worked");
}
private static void Main()
{
new Program().run();
}
}
}
You could experiment with a mixture of implicit conversions and operator overloading, but from my experience you will never make it work as seamlessly as you wish (and as you could get it to work in C++).
If I were you, I would change the getCurrentValue to a property:
public int CurrentValue
{
get {return currValue};
}
and just use someClassInstance.CurrentValue -5
With this code:
public partial class Form1 : Form
{
private static readonly int TABCONTROL_BASICINFO = 0;
private static readonly int TABCONTROL_CONFIDENTIALINFO = 1;
private static readonly int TABCONTROL_ROLESANDSECURITY = 2;
private static readonly int TABCONTROL_INACTIVEINFO = 3;
. . .
int ActiveTabPage = tabControlWorker.SelectedIndex;
switch (ActiveTabPage) {
case TABCONTROL_BASICINFO:
if (currentNode == "NodeBuckingham") {
} else if (currentNode == "NodeNamath") {
} else if (currentNode == "NodeParsons") {
} else {
}
break;
...I have to replace "TABCONTROL_BASICINFO" with "0", or I get, "A constant value is
expected"
Heavens to Murgatroyd! Can't it look up and see that TABCONTROL_BASICINFO is 0?
If you want to make it a constant expression as far as the compiler is concerned, declare it as a const:
// Note that static readonly is implied here
private const int TABCONTROL_BASICINFO = 0;
Or follow .NET naming conventions...
private const int TabControlBasicInfo = 0;
Or use an enum, given that you've basically got a fixed set of values:
private enum TabControlType
{
// Could ditch the explicit values here if you want
BasicInfo = 0,
ConfidentialInfo = 1,
...
}
By the way, you can also switch on strings in C#, so this:
if (currentNode == "NodeBuckingham") {
} else if (currentNode == "NodeNamath") {
} else if (currentNode == "NodeParsons") {
} else {
}
Can become:
switch (currentNode) {
case "NodeBuckingham":
...
break;
case "NodeNamath":
...
break;
case "NodeParsons":
...
break;
default:
...
break;
}
A readonly variable is not a constant. The value is not known at compile time, but rather can be initialized either in the declaration (as you have done) or in the class constructor (in this case, the static constructor for your class).
For more see
http://msdn.microsoft.com/en-us/library/acdd6hb7(v=vs.71).aspx
You can change it to:
private const int TABCONTROL_BASICINFO = 0;
Unless you need to compute something to initialize the variable, declare it as const. It will be slightly more efficient.
That's not a constant; it's a read-only variable. If you want it to be a constant then mark it as a constant.
private const int TabControlBasicInfo = 0;
Or even better:
private enum TabControl
{
BasicInfo,
...
}
...
switch(whatever)
{
case TabControl.BasicInfo:
Static readonly isn't a constant variable. It can be initialized. Whereas "const" is constant.
class SomeClass
{
private struct PhraseInfo
{
public int Start;
public int Length;
}
...
private void SomeMethod(...)
{
List<PhraseInfo> posesBracket = new List<PhraseInfo>();
posesBracket.Add(new PhraseInfo());
posesBracket[0].Start = 10;
}
of cause, posesBracket[0].start=10; occur compiler error CS1612 : "Cannot modify the return value of 'expression' because it is not a variable"
how can i modify a value in list?
The problem is that PhraseInfo is a value type, so the this[] method will return a value, not a reference, to solve it, do this:
PhraseInfo pi = posesBracket[0];
pi.Start = 10;
posesBracket[0] = pi;
var temp = posesBracket[0];
temp.Start = 10;
posesBracket[0] = temp;
You cannot have a struct defined as a method. And as they say, you need the reference to change values. So it goes like this:
class SomeClass
{
private struct PhraseInfo
{
public int Start;
public int Length;
}
private void somemethod()
{
List<PhraseInfo> posesBracket = new List<PhraseInfo>();
posesBracket.Add(new PhraseInfo());
PhraseInfo pi = posesBracket[0];
pi.Start = 10;
posesBracket[0] = pi;
}
}
Right now my class is set up as:
enum Unit{Inches,Centimeters};
Later on I have a step that sets all of the properties of each of these units into my classes instance variables. For example:
int unitBase; //10 for metric, 16 for american
int label;
switch(unit)
{
case Unit.Inches: unitBase = 16; unitLabel = "\"";break;
case Unit.Centimeters: unitBase = 10; unitLabel = "cm";break;
}
I would rather store this all in a Unit class or struct. Then, I would like to be able to access it in the same way you access colors, for example. You say Color.Blue for a blue color, I would like to say Unit.Inches for inches... That is why I dont make a unit base class and simply extend it.
I know that there is a way to do this! Could anyone enlighten me?
Thanks!
You can use static properties:
public enum UnitSpecifier{Inches,Centimeters};
public class Unit
{
int unitBase; //10 for metric, 16 for american
string unitLabel;
public Unit(UnitSpecifier unit)
{
switch (unit)
{
case UnitSpecifier.Inches: unitBase = 16; unitLabel = #"\"; break;
case UnitSpecifier.Centimeters: unitBase = 10; unitLabel = "cm"; break;
}
}
public static readonly Unit Inches = new Unit(UnitSpecifier.Inches);
}
public struct Unit {
public static readonly Unit Inches = new Unit(16, "\"");
public static readonly Unit Centimeters = new Unit(10, "cm");
private readonly int _unitBase;
private readonly string _unitLabel;
static Unit() { }
private Unit(int unitBase, string unitLabel) {
this._unitBase = unitBase;
this._unitLabel = unitLabel;
}
public int UnitBase {
get { return this._unitBase; }
}
public string UnitLabel {
get { return this._unitLabel; }
}
}