0

I have a class like Quete (first in , first out)

public class Tor<T>
{
    private T[] elements;

    private int next;

    public event EventHandler<EventArgs> queueFull;   
}

I need to build a Constructor that get one parameter (queue size (int). and it allocate the arrary and initilizes the variable,

how to do it?

Antonio MG
  • 20,382
  • 3
  • 43
  • 62

5 Answers5

2

What about simple

public Tor(int size)
{
  elements = new T[size];
}
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
1
public class Tor<T> where T : new()
{
    public Tor(int size)
    { 
        elements = new T[size];
        for (int i = 0; i < size; i++)
        {
            elements[i] = new T();
        }
    }

    private T[] elements;

    private int next;

    public event EventHandler<EventArgs> queueFull;   
}

or

public class Tor<T> where T : new()
{
    public Tor(int size)
    { 
        elements = Enumerable.Range(1,size).Select (e => new T()).ToArray(); 
    }
    ...
}
Phil
  • 42,255
  • 9
  • 100
  • 100
0
public class Tor<T>
{
    private T[] elements;

    private int next;

    public event EventHandler<EventArgs> queueFull;

    public Tor(int capacity)
    {
        elements = new T[capacity];
    }
}

should do it.

Emond
  • 50,210
  • 11
  • 84
  • 115
0
public class Tor<T>
{
    private T[] elements;

    private int next;

    public event EventHandler<EventArgs> queueFull;

    public Tor(int size)
    {
        if (size < 0)
            throw new ArgumentOutOfRangeException("Size cannot be less then zero");

        elements = new T[size];
        next = 0;
    }
}
0

Consider using existing classes

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179