-1

(There's not an actual function where I need this, but I was just wondering.)

Imagine this function where I pass a bool[,]. This bool[,] is named grid1 or grid2, depending on the situation.

I was wondering if I can do anything like the following:

void CheckGrid(bool[,] grid, int number)
{
    for (int x = 0; x <= gridXmax - 1; x++)
    {
        for (int y = 0; y <= gridYmax - 1; y++)
        {
            if(grid + number[x,y]) //this will check against grid1 or grid2, depending on int number
                //logic depends on whether it's grid1 or grid2
        }
    }
}

Guessing by the questions for other languages, it probably isn't possible. But you never know :)

It is well possible I'm missing something obvious here - I'm not really experienced.

Simon Verbeke
  • 2,905
  • 8
  • 36
  • 55

5 Answers5

3

You can create an array of grids, then use the number value to check that.

List<bool[,] grids = new List<bool[,]>();

then

if (grids[number][x,y])...
Nick Vaccaro
  • 5,428
  • 6
  • 38
  • 60
2

No - an object doesn't have a name, only a variable has a name. So although you pass a reference to an array, there's no way that the method can know whether you happened to use a variable called grid1 for the argument or a variable called grid2.

Usually when there are questions like this, the answer involves saying that you can use reflection to access member variables by name, but it's generally a bad idea to do so - and that using a single variable which is a collection is a better idea. However, in your question it's pretty unclear what you're trying to do anyway... if it is trying to determine "the name of the object" then that's definitely infeasible in general.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • My example was indeed bad, I'm trying to think of new example :) – Simon Verbeke Feb 10 '12 at 20:47
  • @SimonVerbeke: Just out of interest, if you can't think of a good example, why is it important to you whether or not it can be achieved? I'd focus on techniques you *actually* want to use :) – Jon Skeet Feb 10 '12 at 20:50
  • I was just pondering about such a thing. And I asked it before thinking thorougly about it. Now I realise there is indeed no way to use this. Except, maybe what Norla said. But that could also be achieved with a regular loop. – Simon Verbeke Feb 10 '12 at 20:55
  • @JonSkeet if the grids are elements of a user interface, the corresponding class might very well have a `Name` property. Novice programmers are sometimes confused by the tendency of designer tools to create a variable named `addressGrid` to refer to a control named "AddressGrid". – phoog Feb 10 '12 at 21:09
1

You'd be better off passing a flag to your function which would allow you to update your logic depending on whether you are dealing with grid1 or grid2.

Ryan
  • 26,884
  • 9
  • 56
  • 83
0

This kind of thing exists in PHP when you use something like this $$var where $var will hold grid1 and turn into $grid1

The only thing I could suggest was using key / value pairs in a dictionary and concatenating the number to 'grid'

JConstantine
  • 3,980
  • 1
  • 33
  • 46
0

Add a dimension to your array.

void CheckGrid(bool[,,] grid, int number)
{
    for (int x = 0; x <= gridXmax - 1; x++)
    {
        for (int y = 0; y <= gridYmax - 1; y++)
        {
            if(grid[number, x,y]) //this will check against grid1 or grid2, depending on int number
                //logic depends on whether it's grid1 or grid2
        }
    }
}  
Blau
  • 5,742
  • 1
  • 18
  • 27