const int amask[16] = {0x8000,0x4000,0x2000,0x1000,0x800,0x400,0x200,0x100,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; const byte mask[6] = {0x20,0x10,0x08,0x04,0x02,0x01};
Defining a constant int array
Started by fdixon, Nov 25 2011 12:14 AM
2 replies to this topic
#1
Posted 25 November 2011 - 12:14 AM
I have been asked to port an Arduino sketch to Netduino. Below is some code we had in the Arduino sketch, and I am finding it difficult to understand how to do this Microsoft .Net Framework. Any advice would be much appreciated. Thanks.
#2
Posted 25 November 2011 - 11:05 AM
int[] amask = {0x8000,0x4000,0x2000,0x1000,0x800,0x400,0x200,0x100,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
byte[] mask = {0x20,0x10,0x08,0x04,0x02,0x01};
when I tried it with const the ide said:
'amask' is of type 'int[]'. A const field of a reference type other than string can only be initialized with null.
Lets face it, embedded software is full of compromise.
STEFF Shield High Powered Led Driver shield.
#3
Posted 25 November 2011 - 12:56 PM
Hello Fdixon.
There's no an exact equivalence for the above snippet.
In the .Net the "const" is much like a "preprocessor" directive, than something concrete.
To mimic the above declaration, you can write as Magpie explained:
However, the "readonly" clause means only that the "amask" reference cannot be changed anymore. You should think it as a pointer instead of a memory block, thus there's no guarantee of immutability for the array cells.
That is:
If you wish a fully immutable collection, you should write by yourself. For instance, by masking your array with an interface or something "dissuasive".
Keep in mind that in the .Net world, almost any object/code is "alive".
Hope it helps.
Cheers
There's no an exact equivalence for the above snippet.
In the .Net the "const" is much like a "preprocessor" directive, than something concrete.
To mimic the above declaration, you can write as Magpie explained:
private static readonly int[] amask = {0x8000,0x4000, /* etc */ ,0x01};
However, the "readonly" clause means only that the "amask" reference cannot be changed anymore. You should think it as a pointer instead of a memory block, thus there's no guarantee of immutability for the array cells.
That is:
amask[3] = 123;is fully legal.
If you wish a fully immutable collection, you should write by yourself. For instance, by masking your array with an interface or something "dissuasive".
Keep in mind that in the .Net world, almost any object/code is "alive".
Hope it helps.
Cheers
Biggest fault of Netduino? It runs by electricity.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users