Reputation: 283
Im having a bit of trouble understanding how to get a two dimensional array from my class with a get.
This is how my classes are currently looking:
class Something
{
private int[,] xArray;
public Something()
{
xArray = new int[var1, var2];
for (int row = 0; row < xArray.Getlength(0); row++)
for (int col = 0; col < xArray.GetLength(1); col++)
xArray[row, col] = someInt;
}
public int[,] XArray
{
get { return (int[,])xArray.Clone(); }
}
}
class Main
{
Something some;
public void writeOut()¨
{
some = new Something();
for (int row = 0; row < some.XArray.GetLength(0); row++)
for (int col = 0; col < some.XArray.GetLength(1); col++)
Console.Write(some.XArray[row, col].ToString());
}
}
When I check with my debugger the xArray have all the values it should in the Something class, but it has no values in the Main class, it only gets the size of the array. what am I doing wrong?
Upvotes: 0
Views: 1946
Reputation: 184
Came up with this snipet and it writes out a hundred "1" in the console, which means the tester (your "Main") does see the right values.
To be completely honnest, I dont have any clue what your problem is since we dont see your whole code. You'll have to figure out yourself unless you post your whole solution. The code you posted does work the way you said it should.
Long story short: I added the pieces needed in order to run, and not only does it run, it doesnt show any bug. You might want to compare your code with the one below.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
tester.writeOut();
}
}
class Something
{
private int firstDimensionLenght = 10;
private int secondDimensionLenght = 10;
private int[,] xArray;
public Something()
{
xArray = new int[firstDimensionLenght, secondDimensionLenght];
for (int row = 0; row < xArray.GetLength(0); row++)
for (int col = 0; col < xArray.GetLength(1); col++)
xArray[row, col] = 1;
}
//Add some intellisence information stating you clone the initial array
public int[,] XArrayCopy
{
get { return (int[,])xArray.Clone(); }
}
}
class tester
{
static Something some;
//We dont want to initialize "some" every time, do we? This constructor
//is called implicitly the first time you call a method or property in tester
static tester(){
some = new Something()
}
//This code is painfuly long to execute compared to what it does
public static void writeOut()
{
for (int row = 0; row < some.XArrayCopy.GetLength(0); row++)
for (int col = 0; col < some.XArrayCopy.GetLength(1); col++)
Console.Write(some.XArrayCopy[row, col].ToString());
}
//This code should be much smoother
public static void wayMoreEfficientWriteOut()
{
int[,] localArray = some.XArrayCopy();
for (int row = 0; row < localArray.GetLength(0); row++)
for (int col = 0; col < localArray.GetLength(1); col++)
Console.Write(localArray[row, col].ToString());
}
}
}
Upvotes: 1
Reputation: 199
From what I understand, clone copy on arrays won't be applied to your elements. You have to do it manually. It suggest doing an extension of Clone and let you manage the deep copy.
Upvotes: 1