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. Struct

Struct

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 5 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.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly) public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... } Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible?

    N C G 3 Replies Last reply
    0
    • B budidharma

      I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly) public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... } Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible?

      N Offline
      N Offline
      NetRocker
      wrote on last edited by
      #2

      I am curious as to why you want to declare the structs inside the class.

      C 1 Reply Last reply
      0
      • B budidharma

        I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly) public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... } Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible?

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        budidharma wrote:

        private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE?

        You're doing two things at once. private struct PersistantData { int m_int1; int m_int2; } PersistantData m_PersistantData; One more thing, unlike C++, C# structs have default access as private, so you'd need to make these int's public if you wanted to use them for anything :-) Christian Graus - Microsoft MVP - C++

        B 1 Reply Last reply
        0
        • N NetRocker

          I am curious as to why you want to declare the structs inside the class.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Plainly because they are for use only by the class, in it's internal workings. Christian Graus - Microsoft MVP - C++

          1 Reply Last reply
          0
          • C Christian Graus

            budidharma wrote:

            private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE?

            You're doing two things at once. private struct PersistantData { int m_int1; int m_int2; } PersistantData m_PersistantData; One more thing, unlike C++, C# structs have default access as private, so you'd need to make these int's public if you wanted to use them for anything :-) Christian Graus - Microsoft MVP - C++

            B Offline
            B Offline
            budidharma
            wrote on last edited by
            #5

            Thanks. I knew I could declare them like that, which is the way it's currently written, I was hoping for a shortcut to define and declare a single instance, since each class simply needs a single instance of each. Not necessary, just a little elegant. And of course, you're correct - they are ONLY used within the internal structure of the class. I'm simply separating the data into catagories that they belong in.

            1 Reply Last reply
            0
            • B budidharma

              I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly) public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... } Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible?

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              You can't declare a struct inside a class, the same way you can't declare a struct inside a struct or a class inside a class. --- b { font-weight: normal; }

              S B 2 Replies Last reply
              0
              • G Guffa

                You can't declare a struct inside a class, the same way you can't declare a struct inside a struct or a class inside a class. --- b { font-weight: normal; }

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                :omg: You can declare classes and structs inside classes, as well as classes and structs inside structs. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                G 1 Reply Last reply
                0
                • S S Senthil Kumar

                  :omg: You can declare classes and structs inside classes, as well as classes and structs inside structs. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Oh, you are absolutely right. Sorry about that. --- b { font-weight: normal; }

                  1 Reply Last reply
                  0
                  • G Guffa

                    You can't declare a struct inside a class, the same way you can't declare a struct inside a struct or a class inside a class. --- b { font-weight: normal; }

                    B Offline
                    B Offline
                    budidharma
                    wrote on last edited by
                    #9

                    ... Yes, you can definately declare a struct inside a class.

                    G 1 Reply Last reply
                    0
                    • B budidharma

                      ... Yes, you can definately declare a struct inside a class.

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      Yes, S. Senthil Kumar already pointed that out. --- b { font-weight: normal; }

                      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