Forum Discussion
Feb 25, 2010
This is a common question that I'll have to write a tech tip up for one day B-).
For a 2-D array, you need to first allocate the size of the first dimension and then for each element in there, you need to allocate the second dimensions. In C it looks something like this (In this case I'm creating an a 2x5 array
long len1 = 2;
long len2 = 5;
Allocate the 1st dimension array (each element will contain the 2nd dimension arrays).
sometype[][] someTypeAofA = new sometype[len1][0];
for(int i=0; i {
Allocate the 2nd dimension array for each entry in the 1st dimension
sometypeAofA[ i ] = new sometype[len2];
for(int j=0; j {
Allocate the actual object for the array entry.
sometypeAofA[ i ][ j ] = new sometype();
// Fill in values in sometype[ i ][ j ]
}
}
Honestly, I'm a big confused on why you are returning a 2-d Array with a single input array. If you are trying to use this as input in one of the iControl methods, we use the first dimension for the main object and then the 2nd dimension for the subobjects (ie. for add pool members, the first dimension would be for each pool and the second dimension would be for all the members for each pool).
[0][0] Pool 1, Member 1
[0][1] Pool 1, Member 2
[0][2] Pool 1, Member 3
[1][0] Pool 2, Member 1
[1][1] Pool 2, Member 2
[1][2] Pool 2, Member 3
If that's the case then you'll you are likely only working with a single pool and you want the first dimension to be of size one and the second dimension to be for each member. Something like this
LocalLBPoolMemberMemberSessionState[][] sessionState = new LocalLBPoolMemberMemberSessionState[1][];
sessionState[0] = new LocalLBPoolMemberMemberSessionState[members.Count];
for (int i = 0; i < members.Count; i++)
{
sessionState[0][ i ] = new LocalLBPoolMemberMemberSessionState();
sessionState[0][ i ].member = new iControl.CommonIPPortDefinition();
sessionState[0][ i ].member.address = members[ i ].IP;
sessionState[0][ i ].member.port = members[ i ].Port;
sessionState[0][ i ].session_state = (CommonEnabledState)state;
}
Now, if you really want the first dimension to be for each member passed in and the second dimension to be of size one for that member, you could do it like this:
LocalLBPoolMemberMemberSessionState[][] sessionState = new LocalLBPoolMemberMemberSessionState[members.Count][];
for (int i = 0; i < Members.Count; i++)
{
sessionState[ i ] = new LocalLBPoolMemberMemberSessionState[1];
sessionState[ i ][0] = new LocalLBPoolMemberMemberSessionState();
sessionState[ i ][0].member = new iControl.CommonIPPortDefinition();
sessionState[ i ][0].member.address = members[ i ].IP;
sessionState[ i ][0].member.port = members[ i ].Port;
sessionState[ i ][0].session_state = (CommonEnabledState)state;
}
Let me know if this answers your question or not. I hope at least some of that made sense B-). BTW, I haven't tested out any of this code so please accept my appologies for any typos.
-Joe