## Resolved PBNI Multi Dimensional Array

0
Undo
1. Ashley Swatton
2. PowerBuilder
3. Monday, 28 October 2019 16:52 PM UTC

Hi,

I'm creating a PBNI using the SDK and C. I would like to know how I can create a multi dimensional pbarray and set it's values. I have a single dimension array working fine but can't figure out what to create a 2 dimensional pbarray.

`pbarray sgpinterface::floatArrayToPBArray(IPB_Session * session, float data[], int size) {`

`pbuint numDimensions = 1; // Set This To 2 For 2D Array`
` PBArrayInfo::ArrayBound bound;`
` bound.lowerBound = 1;`
` bound.upperBound = size;`

`pbarray dataArray = session->NewBoundedSimpleArray(pbvalue_dec, numDimensions, &bound);`
` pblong dim[1];`

`for (int i = 0; i < size; i++) {`
` dim[0] = i + 1; // What Should This Be For 2D Reference?`
` float floatVal = data[i];`
` char b[100];`
` sprintf_s(b, 100, "%f", floatVal);`
` pbdec decval = session->NewDecimal();`
` session->SetDecimal(decval, (LPCSTR)b);`
` session->SetDecArrayItem(dataArray, dim, decval); // How To Use Dim Here To Set 2D Array Value?`
` }`
` return dataArray;`
`}`

Responses (5)
Roland Smith Accepted Answer Pending Moderation
1. Thursday, 31 October 2019 13:14 PM UTC
2. PowerBuilder
3. # 1

The PBNI documentation is on the thin side.

Perhaps the reason that the argument 'dim' is an array is because that is how you specify the additional dimensions.

Since C\C++ arrays are zero-based, dim[0] refers to the first dimension. I haven't tried it but it could be that dim[1] would be the second dimension, dim[2] the third dimension, and so on.

I recently finished a PBNI project that used arrays but I didn't have a need for multi dimensions.

When you try it, remember to define it as dim[2] for two dimensional.

Comment
Ashley Swatton Accepted Answer Pending Moderation
1. Tuesday, 29 October 2019 09:18 AM UTC
2. PowerBuilder
3. # 2

Hi, thanks, but maybe i didn't explain myself properly! I was referring to the C side of things using the PBNI SimpleBoundedArray object from the SDK. When you call NewBoundedSimpleArray the 2nd parameter is the number of dimensions but I can't figure out how you set the values for a 2 dimensional array. The code I have above works fine for a single array which is being returned back to PowerBuilder but I have a need for a 2D array.

Comment
1. Miguel Leeuwe
2. Tuesday, 29 October 2019 09:22 AM UTC
Okay, so I think that's the part of Olan's answer where he might be correct in saying you would have to use a powerbuilder object? ... though of course that doesn't solve your problem on the "C" side.

Maybe someone else can help you with that hopefully.

regards
1. Miguel Leeuwe
2. Tuesday, 29 October 2019 09:24 AM UTC
I think that the expert on this is Roland S., but if I'm not wrong, he's busy attending Elevate 2019 at this very moment.:(
Miguel Leeuwe Accepted Answer Pending Moderation
1. Tuesday, 29 October 2019 00:33 AM UTC
2. PowerBuilder
3. # 3

Comment
1. Olan Knight
2. Tuesday, 29 October 2019 03:08 AM UTC
Argh! You are correct! My apologies.

I simulate this with a structure of arrays.

Olan

1. Miguel Leeuwe
2. Tuesday, 29 October 2019 08:55 AM UTC
No worries Olan, no one's perfect, though almost :)
Olan Knight Accepted Answer Pending Moderation
1. Tuesday, 29 October 2019 00:08 AM UTC
2. PowerBuilder
3. # 4

A standard UNBOUNDED array declaration is ll_array []

Try it out!

And yes, there are limitations on all arrays.

Olan

Comment
Olan Knight Accepted Answer Pending Moderation
1. Monday, 28 October 2019 18:53 PM UTC
2. PowerBuilder
3. # 5

Standard PowerBuilder will allow 2 or more dimensions in an array by declaring each dimension with "[]" as follows:

long   ll_long_array []

string   ls_string_array [5, 7]

As stated in my revised comment above,ll_array [][][] is simulated with a structure of one dimensional arrays.
My apologies for getting so confused!

However, you cannot pass multi dimensional arrays as parameters; use structures instead.

Later -

Olan

Comment
1. Miguel Leeuwe
2. Monday, 28 October 2019 21:31 PM UTC
Sorry Olan, but I think that's wrong. The correct declaration should be:

long ll_long_array [4,5,6] // 3 dimensional fixed dimensions array example

long ll_long_variable[] // 1 dimensional variable dimension array

As far as I know you cannot create a variable length multidimensional array.

(The fact that multidimensional arrays have to be of a predefined size has never made them very useful to me).

For more complicated stuff I prefer using a datastore which has the advantage of being able to sort, filter, find, etc.

1. Miguel Leeuwe
2. Monday, 28 October 2019 22:05 PM UTC
.. unless you're referring to the "C" side, but then still, multidimensional arrays need a fixed amount of elements, (correct me if I'm wrong). for example:

int multi[4][5][6];