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. Properties in structures

Properties in structures

Scheduled Pinned Locked Moved C#
question
7 Posts 3 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.
  • C Offline
    C Offline
    chettu
    wrote on last edited by
    #1

    Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg. struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } } This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005

    X D 2 Replies Last reply
    0
    • C chettu

      Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg. struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } } This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005

      X Offline
      X Offline
      XRaheemX
      wrote on last edited by
      #2

      Try this:

      	        struct MyStruct
      		{
      			int fieldx;
      			public int Propertyx
      			{
      				get
      				{
      					return fieldx;
      				}
      				set
      				{
      					fieldx = value;
      				}
      			}
      
      			public MyStruct(int i)
      			{
      				fieldx = i;
      			}
      		}
      
      D 1 Reply Last reply
      0
      • C chettu

        Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg. struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } } This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005

        D Offline
        D Offline
        David Stone
        wrote on last edited by
        #3

        It looks like, from reading the specification, you're not allowed to utilize any instance members of the type until after it's been fully initialized (viz. all of its consituent fields have been initialized). And since Propertyx is an instance member and fieldx hasn't been set yet, you can't use Propertyx. I could be wrong through. Try setting fieldx and then using Propertyx...see what that gives you. :)


        Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

        1 Reply Last reply
        0
        • X XRaheemX

          Try this:

          	        struct MyStruct
          		{
          			int fieldx;
          			public int Propertyx
          			{
          				get
          				{
          					return fieldx;
          				}
          				set
          				{
          					fieldx = value;
          				}
          			}
          
          			public MyStruct(int i)
          			{
          				fieldx = i;
          			}
          		}
          
          D Offline
          D Offline
          David Stone
          wrote on last edited by
          #4

          He did say that he was able to assign directly to the field. He just wanted to know why he couldn't assign to the property. ;)


          Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

          X 1 Reply Last reply
          0
          • D David Stone

            He did say that he was able to assign directly to the field. He just wanted to know why he couldn't assign to the property. ;)


            Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

            X Offline
            X Offline
            XRaheemX
            wrote on last edited by
            #5

            Ahhh.. I read right over that... Well, you can assign the property as long as you assign the field also. When building a struct constructor, the constructor must not exit until all fields have been assigned. Of course, you're thinking to yourself, "It does get assigned when the property is assigned"... unfortunately the compiler doesn't see that :(

            D 1 Reply Last reply
            0
            • D David Stone

              Right. And that's how I answered him. ;) I can see the logic behind the compiler not looking through the property to see that the field gets initialized. For instance, what if your property setter utilized another field in the struct that hadn't been initialized yet? If you just make it a hard and fast rule of structs that they must have all their fields initialized before you use any of them, then it simplifies the work that the compiler has to do. There's always reference types. ;)


              Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

              X Offline
              X Offline
              XRaheemX
              wrote on last edited by
              #6

              Yessir :laugh:

              1 Reply Last reply
              0
              • X XRaheemX

                Ahhh.. I read right over that... Well, you can assign the property as long as you assign the field also. When building a struct constructor, the constructor must not exit until all fields have been assigned. Of course, you're thinking to yourself, "It does get assigned when the property is assigned"... unfortunately the compiler doesn't see that :(

                D Offline
                D Offline
                David Stone
                wrote on last edited by
                #7

                Right. And that's how I answered him. ;) I can see the logic behind the compiler not looking through the property to see that the field gets initialized. For instance, what if your property setter utilized another field in the struct that hadn't been initialized yet? If you just make it a hard and fast rule of structs that they must have all their fields initialized before you use any of them, then it simplifies the work that the compiler has to do. There's always reference types. ;)


                Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

                X 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