Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Setting readonly values in structs within my struct

Setting readonly values in structs within my struct

Scheduled Pinned Locked Moved C#
questionhtmlhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle public struct foo   {   public readonly List{{frog}} al;   public struct frog   {     public readonly int legs;     public readonly int eyes;   }   public foo(int unususedInt)   {     al = new List{{frog}}();     frog myFrog = new frog();      myFrog.legs = 2;     myFrog.eyes = 2;     al.Add(myFrog);   } } What am I doing wrong, and how can I implement this?

    W 2 Replies Last reply
    0
    • J JoeRip

      I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle public struct foo   {   public readonly List{{frog}} al;   public struct frog   {     public readonly int legs;     public readonly int eyes;   }   public foo(int unususedInt)   {     al = new List{{frog}}();     frog myFrog = new frog();      myFrog.legs = 2;     myFrog.eyes = 2;     al.Add(myFrog);   } } What am I doing wrong, and how can I implement this?

      W Offline
      W Offline
      Wil Peck
      wrote on last edited by
      #2

      JoeRip wrote:

      public foo(int unususedInt) { al = new List{{frog}}(); frog myFrog = new frog(); myFrog.legs = 2; myFrog.eyes = 2; al.Add(myFrog); }

      It looks like you have the legs and eyes fields on your struct set as readonly. I doubt the error you're encountering is being generated from writing to the list, but instead setting the properties on the frog instance. Hope this helps.

      1 Reply Last reply
      0
      • J JoeRip

        I want to keep the values in "frog" readonly. However, for some reason I'm not being allowed to set these values in my constructor, even though the error reads: A readonly field cannot be assigned to (except in a constructor or a variable initializer) *Note: {{ and }} replace angle brackets, which HTML can't handle public struct foo   {   public readonly List{{frog}} al;   public struct frog   {     public readonly int legs;     public readonly int eyes;   }   public foo(int unususedInt)   {     al = new List{{frog}}();     frog myFrog = new frog();      myFrog.legs = 2;     myFrog.eyes = 2;     al.Add(myFrog);   } } What am I doing wrong, and how can I implement this?

        W Offline
        W Offline
        Wil Peck
        wrote on last edited by
        #3

        Sorry, I misread the first time.... Since those fields are readonly in addition to your list, you need to set them in the a constructor of the frog struct. You might try adding something like the following to your frog struct. public frog(int legs, int eyes) { this.legs = legs; this.eyes = eyes; } so your entire program would look like.... public struct foo { public readonly List<frog> al; public struct frog { public frog(int eyes, int legs) { this.eyes = eyes; this.legs = legs; } public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List<frog>(); al.Add(new frog(2, 2)); } } Hope that solves your prob. Sorry it took me twice to get the problem. :)

        J 1 Reply Last reply
        0
        • W Wil Peck

          Sorry, I misread the first time.... Since those fields are readonly in addition to your list, you need to set them in the a constructor of the frog struct. You might try adding something like the following to your frog struct. public frog(int legs, int eyes) { this.legs = legs; this.eyes = eyes; } so your entire program would look like.... public struct foo { public readonly List<frog> al; public struct frog { public frog(int eyes, int legs) { this.eyes = eyes; this.legs = legs; } public readonly int legs; public readonly int eyes; } public foo(int unususedInt) { al = new List<frog>(); al.Add(new frog(2, 2)); } } Hope that solves your prob. Sorry it took me twice to get the problem. :)

          J Offline
          J Offline
          JoeRip
          wrote on last edited by
          #4

          Interesting, I'll try this. Thanks for your help. Man, I have to get a better grip on the difference between the instance data and the... uh, non-instance data. Apparently all of my questions about using a struct come down to this.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups