Ensure Variable Initialization C# - c#

Consider this code:
public string Variable1 { get; set;}
public int Variable2 { get; set;}
public void Function()
{
// Has been Variable1 Initialized?
}
Inside the function, I want to know if a value has been sent to Variable1 & Variable2, prior to the function call,
even if the DEFAULT values have been sent, that's ok (null for string & 0 for int)

Consider using a simple wrapper like this:
public struct AssignableProperty<T>
{
private T _value;
public T Value
{
get { return _value; }
set
{
WasAssigned = true;
_value = value;
}
}
public bool WasAssigned { get; private set; }
public static implicit operator AssignableProperty<T>(T data)
{
return new AssignableProperty<T>() { Value = data };
}
public static bool operator ==(AssignableProperty<T> initial, T data)
{
return initial.Value.Equals(data);
}
public static bool operator !=(AssignableProperty<T> initial, T data)
{
return !initial.Value.Equals(data);
}
public override string ToString()
{
return Value.ToString();
}
}
Then your class'll look like this:
public class Test
{
public AssignableProperty<string> Variable1 { get; set; }
public AssignableProperty<int> Variable2 { get; set; }
public void Function()
{
if(Variable1.WasAssigned&&Variable2.WasAssigned)
//do stuff
}
}
You can go further and add throw Exception or contract to getter, so if somebody'll try to access uninitialized value it'll throw an exception or show you warning

Some basics about default value in C#:
When an instance of a class (or struct) is created, all fields are initialized to their respective default value.
For reference types, it will be null. For value types, it will be equivalent to 0. This is easily explains as the memory management ensures that new allocated memory is initialized to 0x0 bytes.
Auto-properties hide the generated field, but there is one. So the same rules apply.
Now to answer your question, the best way to make sure that values are initialized is to make a constructor with one parameter for each field/property and to hide the default constructor with no parameters:
public Yourtype(String param1, Int32 param2)
{
this.Variable1 = param1;
this.Variable2 = param2;
}
private Yourtype() { }
Other alternatives is described in #Sean and #Alex answers if only a subset of properties/fields needs to be initialized/checked. But this hides some overhead (one bool for each property/field and some indirection).

For the reference types you'll need to add a flag:
string m_Variable1;
bool m_IsVariable1Set;
public string Variable1
{
get{return m_Variable1;}
set{m_IsVariable1Set = true; m_Variable1 = value;}
}
For the value types you can use a nullable value
int? m_Variable2;
int Variable2
{
get{return m_Variable2.GetValueOrDefault();}
set{m_Variable2 = value;}
}
Which you can then check to see if it's been set by using m_Variable2.HasValue.

Well you can simply do a check on both variables to see if they have any value assigned to them in your function
public void Function()
{
if (String.IsNullOrEmpty(Variable1) && Variable2 ==0 )
{
// Variables are not assigned
}
}

Related

C# Check which Parameters are set in an Named-Method [duplicate]

This question already has answers here:
Check inside method whether some optional argument was passed
(10 answers)
Closed 2 years ago.
I hope someone can help me or give me an information if that is even possible...
I want to check in an Named-Method which parameters are really set and passed to it.
It would be possible to make an Dictionary and pass the parameter by an KeyValue-Pair to the Method, but is there an other solution?
Can i check the current method in the stack-trace and collect the current set argmuents of that method or something?
For better understanding, i created an example to visualize the issue:
class Program
{
static void Main(string[] args)
{
TestClass newC = new TestClass("Init", 10);
newC.ToString(); //{varStr: "Init"; varInt: 10}
newC.update(vStr: "ok");
newC.ToString(); //{varStr: "ok"; varInt: 0}
// !!! but should have {varStr: "ok"; varInt: 10} !!!
Console.WriteLine("<-- press any key to exit -->");
Console.ReadKey();
}
}
class TestClass
{
string varStr;
int varInt;
public TestClass(string vStr, int vInt)
{
varStr = vStr;
varInt = vInt;
}
public void update(string vStr = default, int vInt = default)
{
//here check it if vStr-Param was set and set varStr only if it was passed to it!
//TODO
varStr = vStr;
//here check it if vInt-Param was set and set varInt only if it was passed to it!
//TODO
varInt = vInt;
}
public override string ToString()
{
Console.WriteLine($"TestClass: varStr: {varStr}; varInt: {varInt};");
return null;
}
}
Is there any way to achieve this, like the way i think?
EDIT:
Use default value and check it!
The default value can also be set over this Method! So an solution
with checking the default value and only if that is equal than we set
the value, is no option for us.
We have also an "custom-class" which we give over that method and it
could also be nullable.
Use Overload methods!
The Problem is that this class has not 2 variables, it has over 15 and
it would be an big overhead to write each overload method.
SOLUTION:
I found a Solution without declare default values... so every Value is able to be set!
class TestClass
{
string varStr;
int varInt;
TestClass1 varCustomClass;
public TestClass(string vStr, int vInt, TestClass1 vCustomClass)
{
varStr = vStr;
varInt = vInt;
varCustomClass = vCustomClass;
}
public void update(Opt<int> varInt = default(Opt<int>),
Opt<string> varStr = default(Opt<string>),
Opt<TestClass1> varCustomClass = default(Opt<TestClass1>))
{
if (varStr.HasValue)
{
this.varStr = varStr.Value;
}
if (varInt.HasValue)
{
this.varInt = varInt.Value;
}
if (varCustomClass.HasValue)
{
this.varCustomClass = varCustomClass.Value;
}
}
public override string ToString()
{
Console.WriteLine($"TestClass3: varStr: {varStr}; varInt: {varInt}; varCustomClass: {varCustomClass};");
return null;
}
}
public struct Opt<T>
{
public Opt(T value)
{
_value = value;
_hasValue = true;
}
public static explicit operator T(Opt<T> optional)
{
return optional._value;
}
public static implicit operator Opt<T>(T value)
{
return new Opt<T>(value);
}
T _value;
public T Value
{
get { return _value; }
}
bool _hasValue;
public bool HasValue
{
get { return _hasValue; }
}
}
Might your string also be null? If not, you can check if it's null. For your int, you can use a nullable value type:
public void update(string vStr = null, int? vInt = null)
{
if(vStr != null)
{
varStr = vStr;
}
if(vInt != null)
{
varInt = vInt.Value;
}
}
If you have only a few parameters and they all have different types, overloaded methods are also a good solution:
public void update(string vStr)
{
varStr = vStr;
}
public void update(int vInt)
{
varInt = vInt;
}
public void update(string vStr, int vInt)
{
update(vStr);
update(vInt);
}

How can I define available values for code completion of a string property in C# in a way that MongoDB.BsonSerializer will understand?

The main purpose is to show intellisense when setting the property. It would be great if I could do it via an attribute like the image below.
The property should remain a string(not enum or struct) so that Mongo's BsonSerializer can serialize it properly. Here is an example of what it might look like:
To help other developers on the team know possible (but not exlusive) values they can use for the Type field Code Completion should display values that can be used as shown below:
(Edited) I was able to solve this by creating my own type
public class SkinType:StringType<SkinType>
{
public SkinType(string value)
{
Value = value;
}
public SkinType()
{
}
public static implicit operator string(SkinType d)
{
return d.Value;
}
public static implicit operator SkinType(string d)
{
return new SkinType(d);
}
public const string StringValue = nameof(StringValue);
public const string Color = nameof(Color);
}
Now I get intellisense for my Type property and Mongo knows how to serialize it.
Here is how I use it:
public class Skin : ServiceMongoIdentity
{
//removed some properties for brevity.
[BsonIgnoreIfDefault]
[BsonDefaultValue(SkinType.StringValue)]
public SkinType Type { get; set; } = SkinType.StringValue;
}
Here is how the StringType base class is defined. I had to make Value public because Generics cannot have constructors with parameters
public abstract class StringType<T> where T :StringType<T>,new()
{
[ReadOnly(true)]
public string Value;
public T FromString(string d)
{
return new T
{
Value = d
};
}
public override bool Equals(object obj)
{
return obj?.ToString() == Value;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Value;
}
}

Can I use constructor logic with object initializer syntax?

i.e.
MyClass myClass = new MyClass() { Value = 5 };
I have a bunch of constructor calls like the one above, but now I've realized I need to add logic to the constructor, which was a massive oversight. Currently I have no constructor, so just a blank implicit default constructor.
The below code should explain my problem.
Edit: I'm not actually doing validation, that's just a simple example of constructor logic
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Test(1) + " should be true");
Console.WriteLine(new Test(0) + " should be false");
Test test = new Test(0) { Value = 1 }; // It allows this syntax, oddly, but the value that's used is the one passed as a parameter
Console.WriteLine("I wish " + test + " was true");
// This is what I have currently, but I'd like to add logic like that which exists in the parameterized constructor
//Test test = new Test() { Value = 1 } // Would ideally function just like Test(1), otherwise I have to go and change every call
// OUTPUT
// True should be true
// False should be false
// I wish False was true
Console.ReadLine();
}
}
class Test
{
public bool? IsGood { get; }
public int Value { get; set; }
// This doesn't currently exist in my class, but I'd like to add it
public Test(int value)
{
if (value == 1)
IsGood = true;
else
IsGood = false;
}
public override string ToString()
{
return IsGood.ToString();
}
}
Don't write code like this in the first place.
I'd write your code like this:
class Test
{
public static bool IsValid(int value)
{
return whatever; // test for validity here
}
public int Value { get; private set; } // Don't let anyone change it.
public Test(int value) {
if (!IsValid(value)) throw new InvalidArgumentException("value");
this.Value = value;
}
}
There, now Value is always valid; the user can know ahead of time whether it is valid or not; an attempt to set an invalid value produces an exception. This assumes that Value cannot change.
If Value can change then write it like this:
class Test
{
public static bool IsValid(int value)
{
return whatever; // test for validity here
}
private int value;
public int Value { get { return value; }
set
{
if (!IsValid(value)) throw new InvalidArgumentException("value");
this.value = value;
}
}
public Test(int value) {
this.Value = value;
}
}
Now the value is again always legal.
If it is legal for value to be invalid, then:
class Test
{
public bool IsValid
{
get
{
return whatever; // test for validity here
} // read-only property
}
public int Value { get; set; }
public Test(int value) {
this.Value = value;
}
}
Now the value can be any integer and whether it is valid or not can be tested dynamically.
Can I set members outside of a constructor while still using logic in the constructor?
Meaning, what, exactly?
Using the object initializer syntax, a constructor still runs. You may even choose which one to use, through the normal constructor overload syntax (which you seem to show, but you say it's not in your class?). The code in your constructor looks at the parameter value that is passed to it, not the property Value (which it doesn't even set). But if you meant for the two to work together, then sure…you can set the property in the constructor and set IsGood in the Value property setter.
If you're going to do it that way, then I would not bother with the logic in the constructor at all. Just set the Value property and let its setter do the rest of the work:
class Test
{
public bool? IsGood { get; private set; }
private int _value;
public int Value
{
get { return _value; }
set
{
_value = value;
IsGood = _value == 1;
}
}
public Test(int value)
{
Value = value;
}
}
I should point out that the semantics of the above is slightly different from what you seem to have started with. That is, the Value property is not read-only, and so can be set at any time. So, similarly, the IsGood property can change at any time. You previously had declared it as read-only and it was settable only in the constructor.
It's not clear from your question whether that's a problem or not. If you want IsGood to be strictly read-only (i.e. without even a private setter), then it won't be possible to do literally what you're asking for, because in the object initializer syntax, it relies on setting member properties after the constructor has already returned.
For the moment, I'll assume it's not a problem to add the private setter to the IsGood property.
Note that since IsGood apparently depends solely on the value of Value, you could even implement the above like this:
class Test
{
public bool? IsGood => _value != null ? _value == 1 : (bool?)null;
private int? _value;
public int Value
{
get { return _value ?? 0; }
set { _value = value; }
}
public Test(int value)
{
Value = value;
}
}
That is, don't even bother storing a value for IsGood. Just return the appropriate value based on the current state of the Value property (null if it's never been set, true if it's currently set to 1, and false otherwise).

Passing variable from another class as input but not getting value back

I have a class where I hold some variables :
public class PreviousCalls
{
private static int bot1Call;
public static int previousBot1Call
{
get { return bot1Call; }
set { bot1Call = value; }
}
private static int bot2Call;
public static int previousBot2Call
{
get { return bot2Call; }
set { bot2Call = value; }
}
private static int bot3Call;
public static int previousBot3Call
{
get { return bot3Call; }
set { bot3Call = value; }
}
private static int bot4Call;
public static int previousBot4Call
{
get { return bot4Call; }
set { bot4Call = value; }
}
private static int bot5Call;
public static int previousBot5Call
{
get { return bot5Call; }
set { bot5Call = value; }
}
}
I need to pass those variables as parameters to a lot of methods in my other class here's how I do it :
void AI(... , int previous)
AI(... , PreviousCalls.previousBot1Call);
So the parameter previous is changing the way it should but the variables from class PreviousCalls are not changing at all, why is that ?
int is value type, so there is a copy of 'previous value' passed to method body. So changing a variable inside method doesn't cause the original value change:
public void Test(int a)
{
a = 10;
}
int t = 11;
Test(t);
//t is still 11, because Test method operates on copy of t
To change original value you must use ref or out:
void AI(..., ref int previous) { ... }
int param;
AI(..., ref param); //when ref is used, original variable wil be changed.
PreviousCalls.previousBot1Call = param;
Unfortunately, you cannot use it like this:
AI(... , ref PreviousCalls.previousBot1Call); // compile-time error
// member-access is forbidden wtih out/ref
AI(,.., ref 10); // compile-time error
Another attempt:
interface IAIParam
{
int Previous { get; set; }
// other params
}
void AI(IAIParam p)
{
p.Previous += 1;
//....
}
And then implementaiton:
internal class MyBotProxy : IAIParam
{
public int Previous
{
get { return PreviousCalls.previousBot1Call; }
set { PreviousCalls.previousBot1Call = value; }
}
}
usage:
var myProxy = new MyBotProxy();
AI(myProxy);
Most commonly methods do not change any values outside of their method scope, instead they return a new value. Only methods that accept the parameter by reference instead of value can change the value of the parameter in the calling context.
This article on MSDN is a great starting point for understanding how to pass parameters by reference instead of value.
Please note that you will not be able to pass a class member as a ref or out parameter. If you wish to update part of a class via reference, you will need to pass the entire class object as the reference.

Automatic Property Value Not Updated with Struct

Take the following struct class:
public struct SingleWraper
{
private double _myValue;
public double MyValue
{
get { return _myValue; }
set { _myValue = value; }
}
public void SetMyValue(double myValue)
{
_myValue = myValue;
}
}
public struct DoubleWraper
{
public SingleWraper SingWraper { get; set; }
public void SetMyValue(double singleVa)
{
SingWraper.SetMyValue(singleVa);
}
}
Run the following test:
[Test]
public void SetMyValue()
{
var singleWraper = new DoubleWraper();
singleWraper.SetMyValue(10);
Assert.AreEqual(10,singleWraper.SingWraper.MyValue);
}
It fails.
However, if you don't use automatic property for DoubleWraper, i.e, you expand the field as below:
public struct DoubleWraper
{
private SingleWraper _singWraper;
public SingleWraper SingWraper
{
get { return _singWraper; }
set { _singWraper = value; }
}
public void SetMyValue(double singleVa)
{
_singWraper.SetMyValue(singleVa);
}
}
Then the test will pass.
Why this is so?
It is here:
_singWraper.SetMyValue(singleVa);
vs:
SingWraper.SetMyValue(singleVa);
In the second, you access a property, therefore you clone the struct; essentially this is the same as:
var clonedAndIndependentValue = SingWraper; // getter
clonedAndIndependentValue.SetMyValue(singleVa);
Note we've updated a different struct value; contrast to field access, which talks to the existing struct value.
Yet another example of why mutable structs are evil. DON'T DO IT. Make the struct immutable (no Set* methods, property setters, or other shenanigans). Or use a class.

Categories