How to create your own primitive data type in .NET?

How to create your own primitive? For example, an integer that has a range from 1 to 10.

EDIT: This was related to the Rosetta Code task.

Defining primitive data types: Demonstrate how to define a type that behaves as a whole but has the lowest allowable value of 1 and the highest allowable value of 10.

I added it here because I thought it might be useful to others.

+4
source share
3 answers
Structure LimitedInt Implements IComparable(Of LimitedInt) Implements IEquatable(Of LimitedInt) Private m_Value As Integer 'treat the default, 0 as being really 1' Public ReadOnly Property Value() As Integer Get Return If(m_Value = 0, 1, m_Value) End Get End Property Public Sub New(ByVal value As Integer) If value < 1 Or value > 10 Then Throw New ArgumentOutOfRangeException("value") End If m_Value = value End Sub Public Function CompareTo(ByVal other As LimitedInt) As Integer _ Implements System.IComparable(Of LimitedInt).CompareTo Return Me.Value - other.Value End Function Public Overloads Function Equals(ByVal other As LimitedInt) As Boolean _ Implements System.IEquatable(Of LimitedInt).Equals Return Me.Value = other.Value End Function Public Overrides Function GetHashCode() As Integer Return Value.GetHashCode End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If TypeOf obj Is LimitedInt Then Return CType(obj, LimitedInt) = Me End Function Public Shared Operator =(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Equals(right) End Operator Public Shared Operator <>(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return Not (left = right) End Operator Public Shared Operator +(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value + right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator -(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value - right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator *(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value * right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator /(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Double Return left.Value / right.Value End Operator Public Shared Operator \(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value \ right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Mod(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Mod right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator And(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value And right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Or(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Or right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator Xor(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As LimitedInt Dim temp As Integer = left.Value Xor right.Value Select Case temp Case 1 To 10 : Return New LimitedInt(temp) Case Else : Throw New OverflowException End Select End Operator Public Shared Operator ^(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Double Return left.Value ^ right.Value End Operator Public Shared Operator <(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value < right.Value End Operator Public Shared Operator >(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value > right.Value End Operator Public Shared Operator <=(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value <= right.Value End Operator Public Shared Operator >=(ByVal left As LimitedInt, _ ByVal right As LimitedInt) As Boolean Return left.Value >= right.Value End Operator Public Shared Widening Operator CType(ByVal left As LimitedInt) As Integer Return left.Value End Operator Public Shared Narrowing Operator CType(ByVal left As Integer) As LimitedInt Return New LimitedInt(left) End Operator End Structure 
+4
source

OK, we will see. First, some data types are built into the CLR. They cannot be changed or new ones added, as they are part of the standard. Here you can find a list here or here . This is C #, but the list must also exist for VB.net somewhere, and it should look equal, because the underlying CLR is the same. Also, the list is not complete because floats and char are missing, but you get the idea.

But then there are some structures that encapsulate these data types and add some additional features. For example, System.Int32 is just a standard structure, without magic. Feel free to look at it in Reflector, this is in mscorlib:

 [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)] public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int> 

So you want your own “1 to 10” Integer? Then I recommend looking at the closest suitable type, which is either Int16 or Byte . If you look at them, you will see that they all look somewhat similar, but they are based on one of the built-in data types.

Just copying / pasting and changing some of the built-in structures (i.e. System.Byte ) does not fully work because some members are internal (i.e. NumberFormatInfo.ValidateParseStyleInteger ), but Reflector can help here.

+7
source

Using Delphi.Net or Delphi Prism (both .Net languages), you can define such variables:

 MyAge : 0..120; NumOfFingersAfterNewYear : 0..10; 

A good advantage is that you get a compile time range check with it.

+2
source

All Articles