Click here to Skip to main content
15,844,498 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to create a structure that defines numerical values ranging from 1to114 same as Int32 etc. work.
I know this approach is heavy but maybe there some way to make the IDE know that there is not only Int32 or Uint32 but also, "XUint", (lol)
How do i achieve this in or C#net, thanks

Public Structure XUInt
  public shared const MinValue = 1
  public shared const MaxValue = 114

...implement casting type CXUInt(i)?

End Structure

Module GlobalRef
 public num1 As XUint = 5 'direct cast
 public num2 As XUint = CXUint(5/2) 'manual casting reference?
End Module

The idea is to let the user know that one has to cast when using a integral Property declared as XUInt.

What I have tried:

Updated 11-Jun-16 13:17pm
Sergey Alexandrovich Kryukov 11-Jun-16 16:15pm    
Why? why?!
Sergey Alexandrovich Kryukov 11-Jun-16 16:43pm    
What exactly do you want to achieve? So far, you try to define it through what you don't want, just assume some absurd thing and say you don't want it. Of course you don't. It does not explain anything.
[no name] 11-Jun-16 17:28pm    
Some time ago, i wrote an external library and declared an Enum in it, in Uint16 type to prevent using unnecessary memory. On the internal Form then i had refered to it in Int32 value, which threw an IndexOutOfRange error exception. I needed more than an hour to allocate the issue. I want to create my own data type as mentioned to tell me at design time that im referecing a wrong data type. Thats it.
Sergey Alexandrovich Kryukov 11-Jun-16 17:40pm    
Why? What this data type should do?
[no name] 11-Jun-16 17:48pm    
It should warn me with the Option Strict On that i should cast the type.
'Strict Off' allows this code to compute: Dim twoandahalf = 5/2
'Strict On' complains and urges to cast: Dim twoandahalf As Integer = CInt(5/2) [=2],
thats what im looking for.
Further sir, i want to implement a casting type also, the like of CInt(x), CSByte(x), CUint(x), do you get the point?

Thank you for your clarifications.

You can define such class, only it's better to make minimum and maximum parameters passed through a constructor, to make it at least a bit universal. Then you can define, say, conversion operators for different integer types (or just one), implicit or explicit. In the implementation of these operator, you can define check up for the valid range. Please see:
Conversion Operators (C# Programming Guide),
Casting and Type Conversions (C# Programming Guide),
explicit (C# Reference),
implicit (C# Reference).

You can add some other operators if you need them, arithmetic for example, with the same kind of the value check.

Share this answer
[no name] 11-Jun-16 19:52pm    
Thank you, that looks promising. I voted 4 and maybe accept this solution when i read them references tomorrow. thanks
[no name] 12-Jun-16 20:38pm    
Thanks again for the answer SA. I understand now what the Conversion Operators achieve, but it is not what i was ultimately searching for. I wanted to create some type of object that really act like the Int32 structure which are static, but Implicit/Explicit Operators are derived from a Class/Struct and are not static and get not removed, collected from GC.

When i write MsgBox(5*2), both operands/integers get erased from memory after the arithmetic operation, but;
MsgBox(CInt(New Xint(5)) * Cint(New Xint(2))) get not erased until i dispose them, because it is invoking dynamic Classes and not static objects that would act like a function.
I admit that the hack i was trying to implement is too much of LowLevel coding which VB IDE does not allow me to do...
Sergey Alexandrovich Kryukov 12-Jun-16 22:37pm    
First of all, nothing is "erased from memory". This is not how it all works. The local variables, if any, are put on stack. When a stack frame is popped, the memory remains intact; it's just new data will overwrite it.

Look, it was you idea to create a class. It takes some space on heap; the memory later will be reclaimed later, but will be deferred. What's wrong about it? Want on-stack behavior and value semantics? Use struct instead of class.

Essentially, what I wrote is what you want, but perhaps, at this moment, this is not what you think you want. You just need to realize it. :-)

Look at modulus: the % operator.
X % n will will always give a result between 0 and n -1
Share this answer
[no name] 11-Jun-16 17:35pm    
Thank you im aware of it, it is like Math.Max/Math.Min, i already was using it, but i was searching for some syntax sugar that would me warn at design time that im referencing a wrong value, because otherwise it gets a pain when referencing a variable on an external library that throws errors only at runtime and with just a few information of what it caused and where it threw.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900