Cant save a list whithin a Stucture

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    Cant save a list whithin a Stucture

    Hello guys, how could I solve this problem?

    Code:
    public struct Alarm
            {
                public int AlarmResetMask;
                List<int> ClassOutput = new List<int>();
                List<int> ClassSize = new List<int>();            
                public int ConfirmCycles;
                public bool Enabled;
                public int FireClassCount;
                public int PointOfReturn;
                public int ZoneCount;
            }
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Originally posted by raulbolanos
    Hello guys, how could I solve this problem?

    Code:
    public struct Alarm
            {
                public int AlarmResetMask;
                List<int> ClassOutput = new List<int>();
                List<int> ClassSize = new List<int>();            
                public int ConfirmCycles;
                public bool Enabled;
                public int FireClassCount;
                public int PointOfReturn;
                public int ZoneCount;
            }
    I'm not sure what you're problem is exactly? If you're getting some compiler error about using a List<T> within a struct then that is news to me, however it does make sense.

    When a struct is created memory is allocated on the heap for it to occupy, if you've got a list though that memory can change, so you don't know how much to allocate.

    You should instead be using a class. Structs should only be used when their size will be no more than 16bytes and they're generally not going to be changed once created.

    Comment

    • balame2004
      New Member
      • Mar 2008
      • 142

      #3
      You can not use user defined types/generic types with in structure. Structure allows only system defined data types.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Please state the problem.

        Originally posted by balame2004
        You can not use user defined types/generic types with in structure. Structure allows only system defined data types.
        I don't see any user defined times in the OP code
        Code:
        public struct Alarm
                {
                    public int AlarmResetMask;
                    List<int> ClassOutput = new List<int>();
                    List<int> ClassSize = new List<int>();            
                    public int ConfirmCycles;
                    public bool Enabled;
                    public int FireClassCount;
                    public int PointOfReturn;
                    public int ZoneCount;
                }
        Just int, List, bool

        Originally posted by raulbolanos
        Hello guys, how could I solve this problem?
        Please define your problem.

        Comment

        • balame2004
          New Member
          • Mar 2008
          • 142

          #5
          I meant generic types (List) used in the structure.I think we can not use generic types in structure. Am I right?

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Don't know: Never tried.

            Any reason you *have* to use a struct? You could use a class instead.

            Comment

            • balame2004
              New Member
              • Mar 2008
              • 142

              #7
              Thanks for your reply. I am using the class only.
              I just tested LIST within structure to know limitations of structure.

              Comment

              • PRR
                Recognized Expert Contributor
                • Dec 2007
                • 750

                #8
                Your List<> are private by default ... therefore wont be accessible via objects..You need to either make them public or use a function( public ) to add, delete etc...
                Structures can hold reference types....

                Comment

                Working...