How to extend System.Int32?
-
Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/
-
Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/
Containing a int32 is your best bet. If you'd like, you can write implicit conversions to and from int32, so you'd be able to write: MyID id = 13; or code like that.
-
Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/
As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).
-
Containing a int32 is your best bet. If you'd like, you can write implicit conversions to and from int32, so you'd be able to write: MyID id = 13; or code like that.
I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/
-
I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/
Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading :-) Anyway, I am in the right direction when I say that: - it's a C++-programmer reflex of mine to expect to be able to overload operator=() - in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as: public static implicit operator MyId(int id) { MyId v = new MyId; v.mId = id; return v; } R/
-
Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading :-) Anyway, I am in the right direction when I say that: - it's a C++-programmer reflex of mine to expect to be able to overload operator=() - in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as: public static implicit operator MyId(int id) { MyId v = new MyId; v.mId = id; return v; } R/
To "override" the = operator, all u need to do is an implicit cast :)
-
As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).
leppie wrote: Search GotDotNet for stoub (his username). I had to look on his blog just to find his username...it's toub...not stoub. ;P
Hawaian shirts and shorts work too in Summer. People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage... -Anna-Jayne Metcalfe on Paintballing
-
I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/
Here's how you overload the conversion from int to your class: public static implicit operator MyId(int id) { return new MyId(id); }