0

I want to create object "A" which will have x, y parameters. and A must be an array. What's best practice of declaring and constructing such object ? for example something like this:

A[0].x=5
A[0].y=3

A[1].x=6
A[1].y=4

Update I've done this way:

    public class PersonalWaypoints
    {
        public float x { get; set; }
        public float y { get; set; }
    }
public class MainClass:MonoBehaviour
{
then in void Start() {
PersonalWaypoints[] pw = new PersonalWaypoints[waypoints.Length];

pw[0].x = waypoints[0].transform.position.x ;
pw[0].y = waypoints[0].transform.position.y ;

but then I cannot use pw in Update() { because it doesn't exist in current context.

And to have it in context I cannot move PersonalWaypoints[] pw = new PersonalWaypoints[waypoints.Length]; to class definition because waypoints.Length is unknown while defining class.

David
  • 4,332
  • 13
  • 54
  • 93
  • 5
    Dont ask others to do your studying for you... read for yourself. https://msdn.microsoft.com/en-us/library/ms173109.aspx –  Mar 22 '16 at 10:44
  • 1
    If you have a form project use Point or PointF which contain an x & y property. You can also use a KeyValuePair. A dictionary is a collection of KeyValuePair so you can use a dictionary to collect all values Dictionary. – jdweng Mar 22 '16 at 11:48

6 Answers6

2

Define a Point type which has your fields:

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

Then create an array of Points (or a List<Point>):

var A = new[] {
    new Point { X = 5, Y = 3 },
    new Point { X = 6, Y = 4 }
};

You can, of course, get there from values for x and y which are not hard-coded into the object creation. For example, say that you have a list xs with integers that should be the x coordinates, and a simliar (and of equal length!) list ys wity y-values. Then, you'll get a list of Points from

var points = xs.Zip(ys, (x,y) => new Point { X = x, Y = y });

Then access the coordinates just the way you wanted: points[1].x is the x-coordinate of the second point.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • How can I create then something like var a = new Point[5] and do a[1].x=1 things later ? – David Mar 22 '16 at 11:44
  • `var a = new Point[5]` allocates the array, but doesn't actually fill it with something - all elements will be null. You will, at some point, have to call `new Point()` for each element in the array. (Of course, you can encapsulate this in a factory method somewhere, but you can't entirely avoid it.) – Tomas Aschan Mar 22 '16 at 13:27
  • I have just a small problem with calling. I have to use it in void Start(){} and void Update(){} Thus I have to call it straight from main class. and it can be done except of when I call new Point[5] instead of 5 I use some variable, and it's inpossible to use variable there during class declaration. – David Mar 22 '16 at 13:43
  • @David: You're referencing a lot of code that you haven't shown. What is the `Start()` and `Update()` methods? What's the context of what you're trying to do? – Tomas Aschan Mar 22 '16 at 15:30
  • http://stackoverflow.com/questions/36119592/objects-using-their-own-unique-waypoints-array/36120910#36120910 Here is the code and what I want to do, but without any luck unfortunatelly :( – David Mar 22 '16 at 15:56
2

You question is a bit vague, if you want autoexpanding array-like collection you have to have two objects: one for point

  //TODO: think over, may be you want a struct, not class
  // May be you want just a standard Point struct
  public class Item {
    public int x {get; set;}
    public int y {get; set;}
  }

and one for "array" (which is an indexer in fact)

  //TODO: you may want to implement IEnumerable<Item>
  public class MyClass {
    private List<Item> m_Items = new List<Item>();

    private void Expand(int index) {
      if (index < 0) 
        throw new ArgumentOutOfRangeException("index");

      for (int i = m_Items.Count; i <= index; ++i)
        m_Items.Add(new Item());
    }

    // indexer: mimics auto expanding array
    public Item this[int index] {
      get {
        Expand(index); 

        return m_Items[index];
      }
    } 

    public int Count {
      get {
        return m_Items.Count;
      }  
    }
  }

...

  MyClass test = new MyClass();
  // test will be expanded to have 1 item: test[0]
  test[0].y = 456;
  // test will be expanded to have 6 items [0..5]
  test[5].x = 123; 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You can create a class with your parameters for it and then create an Array or List of the class:

public class _A
{
    public int x;
    public int y;
} 

_A[] A;

Or

List<_A> = new List<_A>();
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0
public class A
{
  int x;
  int y;
}

A[] numbers = new A[10];
Eminem
  • 7,206
  • 15
  • 53
  • 95
0
sealed class A {
    readonly int m_x;
    readonly int m_y;

    internal A(int x, int y) {
        m_x = x;
        m_y = y;
    }

    internal int X {
        get {
            return m_x;
        }
    }

    internal int Y {
        get {
            return m_y;
        }
    }
}

And then to get array of objects:

 var myVar = new A[1]; // Array size between []
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
0

First you must make a class Coordinate

class Coordinate{
public int x;
public int y;
public Coordinate(int x, int y){
this.x=x;
this.y=y;
}

After that you create an array of Coordinates

Coordinate[]A=new Coordinate[10];
A[0]=new Coordinate(0,0);
A[1]=new Coordinate(5,4);