I have an error with the constructor in this structure:
The compiler is complaining about the constructor because it is parameterless, and a parameterless constructor always gets created by default for a 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; } } }
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.





Reply With Quote