1

I am trying to create a class with a property that is a two-dimensional array. The array will hold various x,y coordinates on a grid (e.g. 0,1 or 3,7) and the size of the array is dependent on a class property called size.

How would you go about creating this array in C#? I have given my solution below, but having very little C# experience and coming from a Python background with some javascript knowledge, it feels like that there is a better solution to this problem.

Could one of you C# wizards enlighten me, please?

Thanks in advance for your help.

Here is my code:

public class Obj
{
    int Size;  // Defines length of array
    int[,] Pos;

    // constructor
    public Obj(int size)
    {
        this.Size = size;
        this.Pos = new int[size, 2];
    }

    public void set_coord(int index, int x, int y)
    {
        if (index >= this.Size) {
            Console.WriteLine("Catch OutOfRangeException");
        }
        else
        {
            this.Pos[index, 0] = x;
            this.Pos[index, 1] = y;
        }
    }
8
  • make a class that also hold coordinates instead of using array. ex you have a class called Coordinate with two properties X and Y. Then you will make 1D array of Coordinate. Commented Nov 11, 2016 at 14:18
  • FYI: In C#, an attribute is something pretty different. The term you should be using here is field. Also, it looks like you're creating the array, so your question is unclear. Can you elaborate some? Commented Nov 11, 2016 at 14:18
  • The code you wrote is not wrong; that is the way of creating a 2D array, but since you're storing x,y coordinates you can just create a 1D array of Points (or create your own struct to hold x and y values) You also might want to learn about C# properties and indexers Commented Nov 11, 2016 at 14:21
  • Seems like he is asking for the best possible solution and I am pretty sure there is none, yours seems to be valid but I like the approach of M.kazem Akhgary more. Commented Nov 11, 2016 at 14:25
  • 1
    First off, thanks so much for your very quick answers. @KevKong The Point class suggested by Dennis_E seems to already exist. What advantages would I have from writing my own Coordinate class like M.kazem Akhgary suggests? Commented Nov 11, 2016 at 14:34

2 Answers 2

2

You could create a List instead of a class, and have an internal sub class to represent your points.

Like this

    public class Obj{
      int Size;
      List<Point> Pos = new List<Point>();

      public Obj(int size){
        this.Size = size;
      }

      public set_coord(int index, int x, int y){
        if(index >= this.Size){
          Console.Writeline("Catch OutOfRangeException")
        }else{
          this.Pos.Add(new Point(x,y));
        }
      }
    }

    class Point{
      int x = 0;
      int y = 0;
      public Point(int xCor, int yCor){
        this.x = xCor;
        this.y = yCor;
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your suggestion! :) What is the advantage of using a List over an array? I had it in my mind that Lists are arrays with no predefined maximum length?
That and they dynamically allocate memory (correctly) when you change its sizes. So that means you as the developer don't need to worry about cleaning up memory. And lets suppose you need a fixed array? Thanks to the IEnumerable<T> prototype in c#, you can just do myList.ToArray()
1

A struct is the ideal approach for this. A full blown class may not be necessary, but it depends.

https://msdn.microsoft.com/en-us/library/ah19swz4.aspx

public struct Coordinates
{  
    public int coordX;  
    public int coordY;  
} 

The property then in your class could be set like this:

var Obj = new Obj();
List<Coordinates> listOfCoords = new List<Coordinates>();
var coord = new Coordinates();
coord.X = 20;
coord.Y = 15
listOfCoords.Add(coord); 
Obj.Pos = listOfCoords

Keep in mind that Structs cannot be inherited from, or inherit, other classes or structs, as well as a few other gotchas. If you need these features, or the data in your struct is prone to modification after it is created (in other words, the data is NOT immutable), consider a small class instead.

https://msdn.microsoft.com/en-us/library/0taef578.aspx

3 Comments

Thanks for your suggestions. I have always been wondering why there are Structs when classes will do all that structs are doing and more... Do you know why?
Structs are intended for small, related groups of properties that are generally not modified. They don't support inheritance. They're also value types (classes are ref types). If your particular case involves changing your coordinate values during the course of the objects usage, the struct isn't really ideal.
I intend for the object to change position at some point... will most likely go for a class then... Thanks for the clarification :)

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.