4

Is there an easy way to specify the variable size and still use them as variables more specifically for the .NETLink?

Something like the following I am thinking.

t = Int[2];

and then the variable could only be set to unsigned int values as an example.

Anoter example I want i = unsignedChar[3] to be exactly 0 to 255 when you assign values to it. So i = 156 would either throw an error or overflow.

William
  • 7,595
  • 2
  • 22
  • 70

1 Answers1

2

Don't know how close this is to what you are after:

ClearAll[t];
Module[{out = True},
 t /: Set[t, n_Integer] /; out := Block[{out = False}, t = n];
 t /: Set[t, whatever : Except[_Integer]] := $Failed
]

You can use whatever patter you wish.

t = 2; t

2

t = "test"
$Failed

There is of course a question about SetDelayed and other methods to set different types of values.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • my question is not very clear but Micheal is close to what I am after. Essentially. I want i = unsignedChar[3] to be exactly 0 to 255 when you assign values to it. So i = 156 would either throw an error or overflow. – William Jan 17 '17 at 16:24
  • So how would I automate the following so if I do t=unsignedChar[2]; it would expand ClearAll[t]; Module[{out = True}, t /: Set[t, n_Integer] /; out := Block[{out = False}, If[0 <= n && n <= 255, t = n, False ] ]; ];t=2; – William Jan 22 '17 at 02:37