I’m building this simple program but i’m having some problems with it. I encapsulated an array into a class and filled it up with random numbers. When in Main i want to evaluate it using Console.WriteLine(), it gives an error:
Cannot apply indexing with [] to an expression of type ‘method group’.
What did i do wrong?
class Program
{
public static void Main(string[] args)
{
Arrays randomArray = new Arrays();
Console.WriteLine("Please type in an integer!");
int encryptionKey = Convert.ToInt32(Console.ReadLine());
randomArray.MyArray.SetValue(encryptionKey, 0);
int i = 0;
while (i < 256)
{
Console.WriteLine(i + " " + randomArray.MyArray[i]);
i++;
}
Console.ReadLine();
}
public static int[] MakeArray()
{
Random rnd = new Random();
var value = Enumerable.Range(0, 256)
.Select(x => new { val = x, order = rnd.Next() })
.OrderBy(i => i.order)
.Select(x => x.val)
.ToArray();
return value;
}
}
public class Arrays
{
private int[] _myArray;
public int[] MyArray
{
get
{
return _myArray;
}
set
{
_myArray = Program.MakeArray();
}
}
}
first of all, in line
program gives you an exception (null reference) becuase in this line the get section of MyArray will executed and returns null to caller. so you must set your array (MyArray) value in the constructor of class (Arrays) and this section is wrong in design by my opinion
try this
this will fix the problem