Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Defining a constant int array


  • Please log in to reply
2 replies to this topic

#1 fdixon

fdixon

    New Member

  • Members
  • Pip
  • 1 posts
  • LocationSydney, Australia

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.


    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};


#2 Magpie

Magpie

    Advanced Member

  • Members
  • PipPipPip
  • 279 posts
  • LocationAustralia (south island)

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 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

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:

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

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.