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

Help with "structs"


  • Please log in to reply
2 replies to this topic

#1 philvr

philvr

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationMelksham, UK

Posted 08 November 2013 - 02:28 PM

I've got the following piece of code public struct MAX6651_DATA { public int nr_fans; public char valid; public uint speed; public uint config; public byte [] tachos = new byte[4]; public uint dac; public uint alarm; } MAX6651_DATA myFan = new MAX6651_DATA(); //create an instance of the fans data structure I would expect... myFan.dac = 100; to assign the dac element the value 100.... Please, what is wrong with this? I must be missing something fundamental! (I fear more background work/reading may be required). Thanks for any advice...

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 November 2013 - 02:59 PM

You can delete '=new byte[4]' to fix '...MAX6651_DATA.tachos': cannot have instance field initializers in structs' compiler error. Alternatively, replace 'struct' with 'class', then you can leave the initializer there.

 

If that does not help, please show the exact error you are getting...



#3 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 20 December 2013 - 05:01 PM

myFan.dac = 100;

to assign the dac element the value 100....

 

When you access a property on a struct, it returns a copy of that value.  When you attempt to change the value, you are changing the value of the copy, not the original.  As far as I know, if you want to change the value, you need to create a new struct with the new value, copying the other values from the original struct.

 

If there isn't a compelling reason to make [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]MAX6651_DATA a struct, then like CW2 suggested, you can just change it to a class.  One reason for keeping it as a struct is if you are going to be creating a large volume of them.  Structs have a smaller memory footprint than classes (which store additional overhead).[/color]


Check out my Netduino projects on GitHub.





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.