0

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?

9
  • what is the value of var1, var2 in this line xArray = new int[var1, var2]; have you used the debugger to step through the code ...? Commented Mar 2, 2015 at 17:31
  • 5
    you seem to be missing a some. in your Console.Write statement, but I doubt that's your problem because your code shouldn't compile with that error. Perhaps you should flesh this example out into a complete program so we can reproduce your error exactly. Note also that you are cloning the array 2 + var1 * var2 times, because it is cloned each time you call the XArray property in the writeOut method. Commented Mar 2, 2015 at 17:32
  • 2
    Side note: returning copy of array is so wrong for intended usage... Commented Mar 2, 2015 at 17:35
  • 1
    I tried to create fiddle of your problem. I don't see anything wrong with it. (I'm using actual values though) Commented Mar 2, 2015 at 17:44
  • 1
    @Rockyy My pleasure. And yes, as people mentioned here, you're creating a clone everythime you're calling XArray. Consider storing the array into a local variable. Commented Mar 2, 2015 at 17:47

2 Answers 2

1

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());
    }

}


}
Sign up to request clarification or add additional context in comments.

6 Comments

yeah, correct, thank you! I made a mistake when assigning variables, but I also wanted to know if it was the correct way to what I wanted.
Tell us what the "Something" class has to do then we might be able to give you a path. You most likely dont want to clone the array, but as long as we dont know your base intent, it is hard to tell what you should do. I'll make sure I read any edit to your question. Just make sure you dont ask two questions at the same time, "philosophy" questions should probably be posted on "Programmers" or "Code Review" depending on the nature of the question.
oh, ok. I just want to get access to the variables in the two dimensional array in "Something". nothing more really.
If you dont care about who can modify the values, you might want to give this a shot: get { return xArray; }. It'll return a pointer to your array, which is the most straightforward way to do what you want.
yeah I don't really care. But if I did, would the Clone() be the right way to do it?
|
1

C# Copy Array by Value

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.

1 Comment

Maybe I got it wrong and didn't understand what you said completely, but isn't that alot more difficult then that it should be? I just want to access the numbers from the two dimensional array in another class. I don't know if I need to use Copy or not haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.