i am trying to compile this code about arrays and classes in visual studio 2010 but i am having problems when running it an error(consoleapplication.Carray does not contain a constructor that takes 0 arguments)is displaying, may someone tell me what wrong i am doing the code only displays and array or is there any way i can do it????
code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
CArray CArray;
CArray nums = new CArray();
for (int i = 0; i <= 49; i++)
nums.insert(i);
nums.displayElements();
}
}
class CArray
{
private int[] arr;
private int upper;
private int numElements;
public CArray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void displayElements()
{
for (int i = 0; i <= upper; i++)
Console.Write(arr[i] + " ");
}
public void clear()
{
for (int i = 0; i <= upper; i++)
arr[i] = 0;
numElements = 0;
}
}
}
OK, several problems. One you have a constructor that is requesting the size of the array, and your main program is not feeding that to the constructor. This is also problematic because you are using the size of the array in your displayelements() loop. This won’t compile since your constructor takes an argument. You need to, at the very least, change your Main() program so that it feeds the size of your array to the constructor, which you have defined in your class CArray. Change the following: