473,725 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multidimensiona l array index lookup in a foreach loop

2 New Member
Expand|Select|Wrap|Line Numbers
  1. Item[,] itemArray;
  2.  
  3. // initialising code here
  4. // fill with data
  5.  
  6. foreach (Item item in itemArray)
  7. {
  8. // I need to access the [x,y] indexes here:
  9. // x_index = ??
  10. // y_index = ??
  11. // item.DoSomething(x_index, y_index);
  12. }
  13.  
Oct 14 '09 #1
2 6248
tlhintoq
3,525 Recognized Expert Specialist
You could do it with a foreach, but given your needs it is is probably easier to you a for with an int counter

Expand|Select|Wrap|Line Numbers
  1. for (int x = 0; x < YourXcounter; x++)
  2. {
  3.    for (int y = 0; y < YourYcounter; y++)
  4.    {
  5.       Item ThisItem = Item[x,y];
  6.       // Do something with ThisItem
  7.    }
  8. }
  9.  
Oct 14 '09 #2
Jaxe
2 New Member
Thanks for the reply.

I'm currently using nested for loops, but they tend to get a bit messy when working with arrays of 3 or more dimensions.

Is there anyway to get it working with the foreach syntax?

I'm looking for something that returns the index for a given dimension.

GetIndex is imaginary here:

Expand|Select|Wrap|Line Numbers
  1. foreach (Item item in itemArray)
  2. {
  3. x_index = item.GetIndex(0); // returns x index
  4. y_index = item.GetIndex(1); // returns y index
  5. z_index = item.GetIndex(2); // returns z index
  6. item.DoSomething(x_index, y_index, z_index);
  7. }
Oct 15 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6754
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
6
15583
by: ubccis | last post by:
Hi. Does for each for on multidimensional arrays? For example, if one element of $in is $course_list and I want to echo all everything in course_list
2
8537
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo "$MyArray"; I get the following: Array
5
4497
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is getting in the way. int a; int b; int **present; int **next;
2
12832
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array, comparing the text in Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
10
12208
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
2
2364
by: phattymatty | last post by:
I am having trouble getting this loop to do what I would like it to. I need to display information from a multidimensional array in order. The order has already been sorted in the array using a usort. How do I display this information the correct way? I previously used mysql_fetch_assoc and returned stored values of win, ties, and losses. Now I have written a function which evaluates each game and determines winners and losers and ties and...
5
2320
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30 =31 )
4
1991
by: Gozil | last post by:
Hello everyone, I have a bit of a problem here, lets me explain the database part first: I want to select pictures from the database and each picture is connected to multi comments. normally I would do this with a foreach loop and then a foreach loop for each comment in each picture like this: $pic = new pictureClass(); foreach($pic->getPictures() as $row) { echo $row; foreach($pic->getPictureComment($row) as $comment) { ...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.