Freeman0x5C Freeman0x5C - 2 months ago 5x
C# Question

Passing An Array Of Objects To A Function

I have a class called "Tiles".

public class Tiles

public int cordX, cordY, tileID;
Booleans someBooleans;
public constructor(int X,int Y,int ID)

The program creates a 2D-grid and stores information (XY-position, tileID) in an array of objects of the Tiles class. Like this:

Create new object array

Tiles[] tileArray = new Tiles[totalGridSize];

Loop through twice to instantiate the individual elements in the object array

tileArray[X + Y] = new Tiles(xPos, yPos, ID);

So far everything is working as expected . But now i want to pass elements of the tileArray to a function. (is it okay for the function to be static?)

public static void tileStatusChecker(Tiles x[])<---

Console.WriteLine("Position is {0}X and {1}Y. ID=={2}", x.cordX, x.cordY, x.tileID)

== This give me the error:

Error CS1552 Array type specifier, [], must appear before parameter name

fair enough, that must be easy to fix.

public static void tileStatusChecker(Tiles[] x)

Console.WriteLine("Position is {0}X and {1}Y. ID=={2}", x.cordX, x.cordY, x.tileID)

Now the compiler is really mad at me: CS1061 'Tiles[]' does not contain a definition for 'tileID' and no extension method 'tileID' accepting a first argument of type 'Tiles[]' could be found (are you missing a using directive or an assembly reference?)

Simultaneously the compiler is also mad at me for calling the function in the first place, declaring the array was never created.


CS0103 The name 'tileArray' does not exist in the current context

I have a standard "learn c# and visual studio" book next to me but object arrays aren't covered at all. And I think the actual error here is me not really knowing what I'm doing. So can anyone tell me how to pass an array of objects to a method? I don't want to just copy+paste the code and have someone fix it for me. I tried to find a solution here on stackoverflow but ended up with 10,214 results after searching for "C# object array to function" or similar.


EDIT: Whoops, I missed a minor detail and answered your question totally backwards. It is clear now that you do not actually want to hand an Array into your function, given that you called your function with a proper accessor reference. If you rewrite your function to take a Tiles rather than a Tiles[], you should be good. All of your problems are arising because your code expects x to be a Tiles while your function declaration expected x to be a Tiles[].

Feel free to ignore the following code chunk, but do definitely hit the note at the end.

Your second shot was better than your first attempt, handing in an array to a function should indeed look like

public static void tileStatusChecker(Tiles[] x)

but your following code, which attempts to get values like "x.cordX", will not find them - Arrays do not have a .CordX; Tiles(s) have a .CordX. If you want to actually get a Tiles, you would want, say, the first (or technically the 'zeroeth') element in x, which is x[0]. E.G. the first Tiles's CordX would be x[0].CordX.

Side-note: It often helps me in writing and talking about my code to choose names that fit the grammatical structure of the thing I'm naming. If it's an object, I usually call it something like Object, e.g. if a class represents a tile I would call it "Tile" rather than "Tiles". If you have a second class that represents a collection of Tiles, like a wrapper around a 2-D array (as I have literally done in the past), then I would name it Tiles.