How do you use .NET Array defined with EXTENT

Posted by jquerijero on 12-Jan-2011 13:21

DEFINE VARIABLE someStrings AS  
  CLASS "System.String[]" EXTENT 3 NO-UNDO.

However when I try to assign the element, I get the following error.

Case 1:
someStrings:SetValue("Hello", 0). - Invalid unsubscripted reference to array element. (3551)

Case 2:
someStrings[1] = ",". - You cannot assign a scalar value to a .NET array object. (14830)

Any ideas?

All Replies

Posted by Admin on 12-Jan-2011 13:27

jquerijero schrieb:

DEFINE VARIABLE someStrings AS  
  CLASS "System.String[]" EXTENT 3 NO-UNDO.

That way you are defining an ABL array (extent 3) of .NET String Array's. Not sure, if that's what you want.

What do you need the array for? I guess you'll either define the variable as CHARACTER extent 3 or "System.String[]". The latter would be instantiated using System.Array:CreateInstance.

Posted by jquerijero on 12-Jan-2011 13:31

That syntax is taken directly from Progress help file. I believe it reads as "array of System.String with initial length of 3". I assume what the syntax is trying to do is to DEFINE and NEW the array in one shot.

Posted by Admin on 12-Jan-2011 13:45

jquerijero schrieb:

That syntax is taken directly from Progress help file. I believe it reads as "array of System.String with initial length of 3". I assume what the syntax is trying to do is to DEFINE and NEW the array in one shot.

Strange. This works; and that is how I learned it...

DEFINE VARIABLE someStrings AS 

  CLASS "System.String[]" NO-UNDO.

someStrings = CAST (System.Array:CreateInstance (Progress.Util.TypeHelper:GetType ("System.String"), 3),

                    "System.String[]") . 

someStrings:SetValue ("Test", 0) .

MESSAGE VALID-OBJECT (someStrings) SKIP

        UNBOX (someStrings:GetValue (0))

    VIEW-AS ALERT-BOX INFO BUTTONS OK.

Or simply use an ABL Character Array.

Posted by jquerijero on 12-Jan-2011 13:52

Yes, the definition without the EXTENT is usuable, but you still need to create the instance and supply the length.

DEFINE VARIABLE someStrings AS "System.String[]" NO-UNDO.

someStrings = NEW "System.String[]" (3). /* this syntax works, you don't need to use CreateInstance */

I'm wondering if the definition with the EXTENT is the supposed replacement for the above two lines.

Posted by Admin on 12-Jan-2011 14:01

I'm wondering if the definition with the EXTENT is a replace for the above two lines.

I doubt it.

Posted by jquerijero on 12-Jan-2011 14:12

Hmm, I think the definition actually meant "an ABL array of .NET array of type System.String". That's going to be interesting.

Posted by Admin on 12-Jan-2011 14:16

Hmm, I think the definition actually meant "an ABL array of .NET array of type System.String".

That was my guess in my first post... But what would you need that for???

Posted by Matt Baker on 12-Jan-2011 15:49

This is valid syntax but it is most likely not what you want.  This  is an ABL array with three entries.  The array can only hold arrays of  .net strings.  The entries in the array have no been initialized yet so  each entry is considered unknown (?).

DEFINE VARIABLE someString AS CLASS "System.String[]" EXTENT 3 NO-UNDO.

You can assign the values but you have to use ABL syntax not .NET syntax since it is an ABL extent variable NOT a .NET object.  This would assign the first entry in the ABL array as a new .NET string array containing a single entry.  The value of which is null.  Note the ABl subscripts start at 1 but .NET subscripts start at 0.

someString[1] = new "System.String[]"(1).

Then you can assign the value of the .NET object like this, probably not what you want.

someString[0].setValue("some value", 0).

If you want a .NET array object you would do this:

define variable someString as "System.String[]" no-undo.

But this would just be a variable, with no object.  You would then need to assign it a value:

someString = new "System.String[]" (3).

Once the variable references a .NET object, then you can assign individual entries to the object array as such:

someString.setValue("some value", 0).

In reality, if you just want a string array of a fixed extent size just define it as an ABL array and you can pass it transparently to a .NET function

define variable someString as character extent 3 no-undo.

someString[1] = "someValue".

This thread is closed