Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] Structure constructor

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] [2.0] Structure constructor

    I have an error with the constructor in this structure:

    Code:
        public struct CommsResponse
        {
            private byte header;
            private byte cmdCode;
            private int length;
            private List<byte> data;
            private byte terminator;
    
            CommsResponse()
            {
                this.Clear();
            }
    
            public void Clear()
            {
                this.header = 0;
                this.cmdCode = 0;
                this.length = 0;
                this.terminator = 0;
    
                if (this.data != null)
                {
                    this.data.Capacity = 1024;
                    this.data.Clear();
                }
                else
                {
                    this.data = new List<byte>(1024);
                }
            }
    
            public byte Header
            {
                get { return this.header; }
                set { this.header = value; }
            }
    
            public byte CommandCode
            {
                get { return this.cmdCode; }
                set { this.cmdCode = value; }
            }
    
            public int Length
            {
                get { return this.length; }
                set { this.length = value; }
            }
    
            public List<byte> Data
            {
                get { return this.data; }
                set { this.data = value; }
            }
    
            public byte Terminator
            {
                get { return this.terminator; }
                set { this.terminator = value; }
            }
        }
    The compiler is complaining about the constructor because it is parameterless, and a parameterless constructor always gets created by default for a structure.

    How should I call the Clear() method when the structure is created? I only really need to do this because of the 'List' field which needs to be created 'new' to initialize it, otherwise I wouldn't need a constructor at all.

    I thought about making a constructor with a parameter that does nothing, but that doesn't seem very elegant somehow.

    Any advice appreciated, thanks.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Structure constructor

    That type should not be a structure. Any type that has a collection as a member should be a class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2.0] Structure constructor

    Quote Originally Posted by jmcilhinney
    That type should not be a structure. Any type that has a collection as a member should be a class.
    Yes, I guess that does make sense actually. Thanks.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width