Racooon
Racooon

Reputation: 1496

Whats wrong with this generic class?

I am trying to do a Buffer Holder for a big buffer stack.

And the classes are:

Buffer Class:

internal class Buffer<T>
{
    private T[] buffer;

    public Buffer(T[] buffer)
    {
        this.buffer = buffer;
    }

    public void clear()
    {
        Array.Clear(buffer, 0, buffer.Length);
    }

    public int length()
    {
        return buffer.Length;
    }
}

DataPool Class:

internal sealed class DataPool<T> : List<Buffer<T>>
{
    public DataPool() : base() {}
}

and how I create the bufferList:

public Server
{
    DataPool<byte[]> bufferList;
    byte[] = buffer;

    public Server(...)
    {     
        buffer = new byte[ServerConfig.MaxBufferSize];
        this.bufferList = new DataPool<byte[]>();
    }

} Everything ok but i cant add buffer into bufferList like:

bufferList.Add(buffer); //This is not working, why?

How can I do it?

Thank you!

Upvotes: 0

Views: 145

Answers (4)

M.Babcock
M.Babcock

Reputation: 18965

It should also be possible to use your code as it is if you implement an implicit type conversion between Buffer<byte[]> and byte[]:

public static implicit operator Buffer<T[]>(T[] ba)  // implicit digit to byte conversion operator
{
    return new Buffer<T[]>(ba);
}

I haven't tested it but something to that effect should be possible.

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

Your buffer is of type byte[] and your bufferList will inherit List<Buffer<byte[]>>.

Try

bufferList.Add(new Buffer<byte[]>(buffer));

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160922

This should work:

var bufferList = new DataPool<byte>();
bufferList.Add(new Buffer<byte>(buffer));

You have to use DataPool<byte> and Buffer<byte> since that causes the Buffer to accept a byte array, which is what you want.

Upvotes: 2

Lucero
Lucero

Reputation: 60190

You need to create the buffer as well:

bufferList.Add(new Buffer<byte[]>(buffer));

Upvotes: 0

Related Questions