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