in C#, internally, a byte is define as something like (sorry I don't have the real code, this is my question):
public class byte {
int bytValue = 0;
get {
return bytValue;
}
set {
if (value < 0 || value > 255) {
throw;
}
bytValue = value;
}
}
Now, let's suppose I want to create such class and call it bytePlus, which would be limited to 0 to 512. How do I create a "Value" class where when called it gives a value?
I might insist [I do not want to overload byte, I'm really using byte as an example, I intend to create a different kind of "value" type class]
Example:
public class bytePlus {
...
}
When I do something like this:
public bytePlus MyValue = 4;
I want MyValue to act like a value. Because, the "default" property would be to return a value. But I want to add also other properties (which is the easy part).
How do I define such class?
I googled to find the answer... no luck
ATTEMPT:
class BytePlus {
private uint _value;
// User-defined conversion from Digit to double
public static implicit operator uint(BytePlus d) {
return d._value;
}
}
when I attempt:
bytePlus rrr = new bytePlus(); rrr = rrr + 1;
it doesn't work, it can't add. "impossible to convert uint to bytePlus()"
My end goal is because there is a string property attached to the "value" that need to stay with the value from the class, when the "real" value is updated, the string value also need to be updated because it represent a kind of crc of the said value.
@shingo proposition was right but I failed to understand it because it needed some more code.
here the code based on MakePeaceGreatAgain accepted Answer
public class BytePlus {
private uint uintValue = 0;
public BytePlus() {
//
}
public BytePlus(uint b) {
this.Value = b;
}
public uint Value {
get {
return uintValue;
}
set {
//here I can add my code...
uintValue = value;
}
}
//here I can add my other properties
public static implicit operator uint(BytePlus d) => d.Value;
public static implicit operator BytePlus(uint b) => new BytePlus(b);
}
then I do:
BytePlus hex32 = 4;
hex32++;
Basically you can add an implict cast to your BytePlus
-type, making the following possible:
BytePlus MyValue = 4;
The other way also works:
byte b = new BytePlus(4);
So you need an operator
for the conversion:
class BytePlus
{
public BytePlus(byte b)
{
this.Value = b;
}
public byte Value { ... }
public static implicit operator byte(BytePlus d) => d.Value;
public static implicit operator BytePlus(byte b) => new BytePlus(b);
}
However be aware that implicit casts imply a loseless conversion. So when converting a byte
to BytePlus
, you essentially add information, which is okay. However the other way is not okay, as a conversion from BytePlus
to byte
may lead to information-loss (a byte
has no knowledge about the members in your BytePlus
-class). In these scenarios it's usually better to make the operator explicit
, which basically means: "there might be information-loss when doing this cast, but I'm okay with it".
See also the docs about user-defined casts: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/user-defined-conversion-operators
When you also want some operator on your type - which means you want to provide +
and -
for instance, you can add so by doing this:
class BytePlus
{
public static BytePlus operator + (BytePlus b, byte amount) => new BytePlus(b.Value + amount);
public static BytePlus operator - (BytePlus b, byte amount) => new BytePlus(b.Value - amount);
}