Reputation: 6030
I want to create my own Integer
with struct
.
Here is a simple example of a Integer
which the return value is forced to between 0 and 255.
These are pseudocode and C# won't compile it.
struct MyInt{
private int p;
default property theInt{
set{
p = value;
}
get{
if(p > 255) return 255; else if( p < 0) return 0;
return p;
}
}
}
My main goal is to use following code :
MyInt aaa = 300; //Grater than 255
if(aaa == 255) aaa = -300; //Less than 255
if(aaa == 0) a = 50;
Is this possible to do with any .NET language ? Of course I prefer C#
Upvotes: 3
Views: 2347
Reputation: 50104
As I said in my comment, you can use an implicit conversion between your structure and int
:
internal struct MyInt
{
private int p;
public int BoundedInt
{
// As CodesInChaos points out, the setter is not required here.
// You could even make the whole property private and jsut use
// the conversions.
get
{
if (p > 255) return 255;
if (p < 0) return 0;
return p;
}
}
public static implicit operator int(MyInt myInt)
{
return myInt.BoundedInt;
}
public static implicit operator MyInt(int i)
{
return new MyInt { p = i };
}
}
You need both the int
-to-struct
conversion for when you assign a value and the struct
-to-int
conversion for when you compare values.
Upvotes: 2
Reputation: 62248
You assign a value to a struct in your code. I don't know what language semantcics you use here but in C#, you can not do that (in VB.NET neither as much as I know).
What you can do it what you actually already defined in your code, so define a property and the logic inside its get
and set
methods.
Yes, there is an option as Rawling suggests to overwrite cast operator between your struct and integer, but please do not do that, it's very confusional and not clear from code what is going on there.
So stand on simple property logic.
public struct MyInt{
private int p = default(int);
public int theInt{
set{
var v = value;
if(v > 255)
v =255;
else if(v < 0)
v = 0;
p = v;
}
get{
return p;
}
}
}
And also please note, that in my example I inverted the logic, I put it into the set
, as if you develope in a way you presented, at some point your p
, will not have a value of the property theInt
, which I would strongly encourage to avoid. If there is a field that holds a property value, it has to be always equal to the value caller will get from the property itself. If not, it creates a confusion, and in long run development: a mess.
Upvotes: 1
Reputation: 108975
.NET does not provide anything like a "default property"1.
As @Rawling comments you can use implicit conversion operators to allow assignment.
But in the end you can never completely emulate the inbuilt types within compiler and .NET CLI, for instance basic operations on System.Int32
are single CLI op-codes and literals are held natively.
1 Except within COM interop, but COM in VB (V6 and before) showed why default properties are a bad idea: having to have two keywords (let
and set
) for assignment to control when the reference or the default property was assigned.
Upvotes: 0
Reputation: 6698
You should use an implicit conversion operator, like this:
public static implicit operator MyInt(int value)
{
return new MyInt(value);
}
This way, you can use MyInt a = 10;
and the value of 10 is assigned in the constructor of a that takes the value as a parameter.
You should then proceed by overloading the other operators.
Upvotes: 0