I’m attempting to set a private field of an array of ints from outside the class through a public property accessor. I’m almost certain the problem is my lack of knowledge of the syntax to get this done. I’ve figured out how to set individual values if I specify the index for the array when accessing the property through the object. Here is what I have so far.
My class below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace paramRefVal
{
class ParamaterTest
{
private int[] _ints = new int[5];
private int _i;
public int[] Ints
{
get { return _ints; }
set { _ints = value; }
}
public int I
{
get { return _i; }
set { _i = value; }
}
public void SomeFunction(int[] Ints, int I)
{
Ints[0] = 100;
I = 100;
}
}
}
This is my main method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace paramRefVal
{
class Program
{
static void Main(string[] args)
{
ParamaterTest paramTest = new ParamaterTest();
paramTest.I = 0;
paramTest.Ints[0] = 99;
Console.WriteLine("Ints[0] = {0}", paramTest.Ints[0]);
Console.WriteLine("I = {0}", paramTest.I);
Console.WriteLine("Calling SomeFunction...");
paramTest.SomeFunction(paramTest.Ints, paramTest.I);
Console.WriteLine("Ints[0] = {0}", paramTest.Ints[0]);
Console.WriteLine("I = {0}", paramTest.I);
Console.ReadLine();
}
}
}
The line I’m interested in is
paramTest.Ints[0] = 99;
I’ve attempted to set multiple values like so to no avail.
paramTest.Ints[] = { 0, 1, 2, 3, 4 };
I’m getting two compilation errors. “The type or namespace name ‘paramTest’ could not be found (are you missing a using directive or an assembly reference?)” without the quotes.
And secondly. “Identifier expected” without the quotes.
Thanks for any help!
You can use:
Which can be simplified to:
If you want to use the array initializer, you can do it this way:
I don’t get any compilation error about the type, though. Could you be more specific?