ObjectType and ArrayLists

Started by spl, April 12, 2024, 07:54:17 AM

Previous topic - Next topic

spl

I was playing with a CLR ArrayList and in the snippet below seemed to have no issues adding or identifying variables created with ObjectType(). I'd like to see if ObjectType of ARRAY could be added such that an ArrayList could contain multiple [ possibly typed ] arrays. Thought I would ask first, as not sure if possible how to implement.
;Winbatch 2022C - CLR ArrayList with ObjectType
;Stan Littlefield April 12, 2024
;////////////////////////////////////////////////////////////////////////////////////////////////////////

ObjectClrOption("useany","System.Net")
oList = ObjectClrNew("System.Collections.ArrayList")
oList.Add("This is a String") 
oList.Add("But") 
oList.Add("Adding a numeric, date and bool") 
oList.Add(500)
dt = ObjectType("DATE", TimeYmdHms() )
oList.Add(dt)
isfalse = ObjectType("BOOL", 0)
oList.Add(isfalse)
nCnt = oList.Count
cTxt="Count: ":nCnt:@CRLF 
cTxt=cTxt:"Capacity: ":oList.Capacity:@CRLF
cTxt=cTxt:"Fixed Len?: ":oList.IsFixedSize:@CRLF 
cTxt=cTxt:"Read only?: ":oList.IsReadOnly:@CRLF
base=0
For i=1 To nCnt
    cTxt=cTxt:"Item":i:" =":oList.Item(base):@CRLF
    base+=1
Next
o1 =  oList.Item(4)
AddTime = "0000:00:00:157:00:00" ; 157 hours
Later=TimeAdd(o1, AddTime)
obj = ObjectTypeGet(o1)
cTxt=cTxt:o1:" =":obj:@CRLF
o2 =  oList.Item(5)
obj = ObjectTypeGet(o2)
cTxt=cTxt:o2:" =":obj:@CRLF
cTxt=cTxt:"Time added to ":o1:" = ":Later:@CRLF
oList=0
Message("ArrayList",cTxt)
Exit 
Stan - formerly stanl [ex-Pundit]

td

An array works as an element of an ArrayList. You don't even need to use ObjecType as WIL can figure the array type out without your assistance.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Understood. I was thinking an array object might be added to Arraylist as a single element with oList.Add(array), but instead needed to use oList.AddRange(array) or InsertRange(position,array) and will work with Wil arrays w/out ObjectType() breaking array into individual ArrayList elements.

[EDIT]:
Played around with it some more. According to Microsoft, Arraylists are deprecated in favor of Generics, but still quite useful, and as I recall from a post and response in 2020, Will cannot handle Generics
;Winbatch 2022C - CLR ArrayList with ObjectType
;Stan Littlefield April 13, 2024
;////////////////////////////////////////////////////////////////////////////////////////////////////////

;elements numeric
array = ArrDimension(5)
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 123456789999999
;elements will be BSTR
list="1,2,3,4,123456789999999"
array1=Arrayize(list,",")

ObjectClrOption("useany","System.Net")
oList = ObjectClrNew("System.Collections.ArrayList")
oList.Add("This is a String") 
oList.Add("But") 
oList.Add("Adding a numeric,date,bool,array")
oList.Add(123456789999999)
dt = ObjectType("DATE", TimeYmdHms() )
oList.Add(dt)
isfalse = ObjectType("BOOL", 0)
oList.Add(isfalse)
oList.AddRange(array)
oList.AddRange(array1)
nCnt = oList.Count
cTxt="Count: ":nCnt:@CRLF 
cTxt=cTxt:"Capacity: ":oList.Capacity:@CRLF
cTxt=cTxt:"Fixed Len?: ":oList.IsFixedSize:@CRLF 
cTxt=cTxt:"Read only?: ":oList.IsReadOnly:@CRLF
base=0
For i=1 To nCnt
    itm = oList.Item(base)
    cTxt=cTxt:"Item":i:"= ":itm:" [Object Type]=":ObjectTypeGet(itm):@CRLF
    base+=1
Next

oList=0
Message("ArrayList",cTxt)
Exit 
Stan - formerly stanl [ex-Pundit]