4
$\begingroup$

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.

$\endgroup$
2
+50
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – William Jan 17 '17 at 16:24
  • $\begingroup$ 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; $\endgroup$ – William Jan 22 '17 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.