overloaded constructor, access zero-argument constructor

  • Thread starter Thread starter mblatch
  • Start date Start date
M

mblatch

This is probably a C#-101 type question, but I've struck out on finding
an answer. I am setting up a class with a few overloaded constructors.
Inside those constructors, I would like to call the "default"
zero-argument constructor. The compiler gives me an error
"PC_StreamBuffer is a type but is used like a variable". I've tried
adding "this." in front of references to PC_StreamBuffer(), but that
gives another compiler error. A simplified version of the class is as
follows:

class PC_StreamBuffer
{
private byte[] data;

public PC_StreamBuffer()
{
length=0;
data=new byte[1000];
}

public PC_StreamBuffer(byte[] vals, int valsLength)
{
PC_StreamBuffer();
this.Set(vals, valsLength);
}

public PC_StreamBuffer(byte[] vals)
{
PC_StreamBuffer();
this.Set(vals);
}

<remainder of class methods removed>
}

Thanks,
Mike
 
mblatch said:
This is probably a C#-101 type question, but I've struck out on finding
an answer. I am setting up a class with a few overloaded constructors.
Inside those constructors, I would like to call the "default"
zero-argument constructor. The compiler gives me an error
"PC_StreamBuffer is a type but is used like a variable". I've tried
adding "this." in front of references to PC_StreamBuffer(), but that
gives another compiler error. A simplified version of the class is as
follows:

<snip>

See http://www.pobox.com/~skeet/csharp/constructors.html
 
You call one constructor from another like this :

public PC_StreamBuffer()
{
length=0;
data=new byte[1000];
}

public PC_StreamBuffer(byte[] vals, int valsLength) : this()
{
this.Set(vals, valsLength);
}

public PC_StreamBuffer(byte[] vals) : this()
{
this.Set(vals);
}
 
Thanks to you both for the quick fix and references. That worked. I
appreciate it.

Mike
 
Back
Top