10 Replies - 305 Views - Last Post: 27 June 2017 - 08:57 PM

#1 fskd37  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 06-March 17

How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 04:13 AM

Hi, in Windows Form I would like to use multi-dimensional list to do as what can be done easily with vector in standard C++ console app as follows. IntelliSense shows that the multi-dimensional List O " function "System::Collections::Generic::List<T>::default[int]::get [ contains T=System::Collections::Generic::List<System::Collections::Generic::List<System::Collections::Generic::List<node ^> ^> ^> ^]" cannot be called with the given argument list.
Actually Visual Studio can still run the program even with IntelliSense notification of errors. However, it'll be better if I can know what's the right or better way to do. Map is vector of nodes (so map[k] is a node),while other variable types don't matter here as the structures look obviously the same (No variable type errors.) Thanks for help!!

int x=0, y=0, z=0;
float range_x = 0, range_y = 0, range_z = 0;	// max - min in 3D respectively
range_x = max.x - min.x;
range_y = max.y - min.y;
range_z = max.z - min.z;
vector <node> O[2][8][8];	// 2*8*8 vectors nodes		
for (unsigned int j = 0; j<map.size(); j++) {
	x = static_cast<int>((map.at(j).x_p - min.x-0.0001) / range_x * 2);	
	y = static_cast<int>((map.at(j).y_p - min.y-0.0001) / range_y * 8);	
	z = static_cast<int>((map.at(j).z_p - min.z-0.0001) / range_z * 8);
	O[x][y][z].push_back(map.at(j));
   }



int x = 0, y = 0, z = 0;
float range_x = max->x - min->x;
float range_y = max->y - min->y;
float range_z = max->z - min->z;

//Initialize size
List <List < List < List <node^> ^ >^ >^ >^ O = gcnew List <List < List < List <node^ >^ >^  >^ >(2);
for (int ix = 0; ix < 2; ix++){
		List <List < List <node^> ^>^>^ temp1 = gcnew List <List < List <node^>^>^ >(8);
		for (int iy = 0; iy < 8; iy++){
			List <List <node^>^>^ temp2 = gcnew List <List <node^>^> (8);	
			for (int iz = 0; iz < 8; iz++){
				List <node^>^ temp3 = gcnew List <node^>();	
				temp2->Add(temp3);
			}
			temp1->Add(temp2);
		}
		O->Add(temp1);
	}

for ( int j = 0; j< map->Count; j++) {
		x = static_cast<short>((map[j]->x_p - min->x - 0.01) / range_x * 2);	
		y = static_cast<short>((map[j]->y_p - min->y - 0.01) / range_y * 8);	
		z = static_cast<short>((map[j]->z_p - min->z - 0.01) / range_z * 8);	
		O[x][y][z] ->Add(map[j]);		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: How to access multi-dimensional list in Windows Form

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 11:34 AM

Why are you doing this?

Why not simply do the following?
List<node^> O[2][8][8];


Was This Post Helpful? 0
  • +
  • -

#3 fskd37  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 06-March 17

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 05:23 PM

Well, I have tried it, but the declaration is not allowed. Maybe it can be done in normal C++ console app, but not in Windows Form. My declaration looks ugly but is correct.

On declaring 2D list in C++ CLI
Another Reference

This post has been edited by Skydiver: 21 June 2017 - 08:36 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 08:39 PM

Sorry I was missing an extra caret. It should be List<node^>^ O[2][8][8];. It shouldn't make a difference between C++ console app or C++ WinForms as long as both are using C++/CLI applications.

Your declaration is not correct if the intent is to use a multidimensional array. There is a difference between an array and a list. Although it may look like you are accessing the items the same way using the square bracket operators, the way the memory is laid out and allocated is actually different.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 09:25 PM

Oh I see now what you are talking about. I forgot about the difference between the old Managed C++ and the current C++/CLI. Yes you are correct that C++/CLI won't allow the declaration of an array of managed types using the unmanaged array. It needs to use the same nesting like you are doing with your lists.

Arrays in C++/CLI

array<array<array<node^>^>^>^  O;


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 09:37 PM

Or even better:
array<Node^, 3>^  O = gcnew array<Node^, 3>(2, 8, 8);


Was This Post Helpful? 0
  • +
  • -

#7 fskd37  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 06-March 17

Re: How to access multi-dimensional list in Windows Form

Posted 21 June 2017 - 10:41 PM

View PostSkydiver, on 21 June 2017 - 09:37 PM, said:

Or even better:
array<Node^, 3>^  O = gcnew array<Node^, 3>(2, 8, 8);



Thanks for your reply! (Sorry for not make the C++ terms clear)
I have considered using array, in which way I can access the elements directly by array. However, notice that my list is 4-dimension and so should the array declaration be. The point is that a seemingly
3-dimension vector vector<node>O[2][8][8] is actually equivalent to a 4-dimension list or array. Simply put, vector<int> num(10) can be expressed as array[10]. Here you can find a problem: vector as a container can add elements whenever we want (I don't know how large each list<node> will be), but array cannot. Also, vector has many more functions than array. List and vector are more similar than array and vector.


vector use in C++ windows forms
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 22 June 2017 - 04:21 PM

Sorry. I forgot that it was 3 dimensional array of vectors. So it should be like this:
array<List<Node^>^, 3>^  O = gcnew array<Node^, 3>(2, 8, 8);


Was This Post Helpful? 0
  • +
  • -

#9 fskd37  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 06-March 17

Re: How to access multi-dimensional list in Windows Form

Posted 25 June 2017 - 05:22 PM

I think it's a good way to declare multi-dimensional array of lists, while your offered declaration is still not accepted. I cannot find what's what should be fixed in the line. Maybe you can try to fix it.
Thank you.

This post has been edited by Skydiver: 25 June 2017 - 07:55 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5778
  • View blog
  • Posts: 19,651
  • Joined: 05-May 12

Re: How to access multi-dimensional list in Windows Form

Posted 25 June 2017 - 07:54 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

The right hand side simply needs to match the types on the left hand side. Currently the inner most list is missing.

array<List<Node^>^, 3>^  O = gcnew array<List<Node^>^, 3>(2, 8, 8);


Was This Post Helpful? 1
  • +
  • -

#11 fskd37  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 06-March 17

Re: How to access multi-dimensional list in Windows Form

Posted 27 June 2017 - 08:57 PM

Now the declaration seems legit, but I received about 20 errors and failed to compile. If O is a 3-dimensional array with data type List<node^>, then it should go well, since what I need is call O[x][y][z] as a normal list like its Add function, Count... etc and its element O[x][y][z][h], but it is not allowed. Well, it's a pity, but thanks for your continual help.

This post has been edited by fskd37: 27 June 2017 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1