I’m creating a custom generic class for an assignment in school. I’m stuck at the moment and i would greatly appreciate some help with the following error.
I’m adding the parameter obj to the array. The problem is that the obj will never change, thus if the object that i is “cake” all indexes in the array will say cake, and not the other string as-well.
class minlist<T>
{
T[] storage = new T[3];
public void Add(T obj)
{
if (storage.Length != 3)
{
storage[0] = obj;
storage[1] = obj;
storage[2] = obj;
storage[3] = obj;
}
I figured that it would solve the problem if i made obj an array as-well. Thus being able sort each string in a different index, i would put cake in 0 and pie in 1 etc.
When i edit the parameter in the public void add(T obj) and make it public void add(T []obj)
I also have the add the indexes for the obj.
storage[0] = obj[0];
storage[1] = obj[1];
storage[2] = obj[2];
storage[3] = obj[3];
In main where i want to add something to the list it usually looks like this, without the changed parameter in the add method.
minlist<authorinfo> aif = new minlist<authorinfo>();
aif.Add(new authorinfo("The Count of Monte Cristo","Alexandre", "Dumas", 1844));
when i change the add parameter to []obj i must i do the following in main.
minlist<authorinfo> aif = new minlist<authorinfo>();
aif.Add(new authorinfo[0]("The Count of Monte Cristo","Alexandre", "Dumas", 1844));
The problem here is that the [0] doesn’t work. I get an error that says method name expected. I got no clue what to do.
Sorry if the question is a-bit confusing.
You should restructure your class. A single
authorinfois not an array ofauthorinfos.Your add method accepts a single item. If you keep track of the location of the next empty item you can create your list: