Results 1 to 26 of 26

Thread: Contents of an arrayList persist through all attempts to dump. WHY?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Contents of an arrayList persist through all attempts to dump. WHY?

    For context, I'm running this code as a macro on an AS400 terminal.
    Offending line in red.

    Entire For Each loop runs through lines in a .txt file and grabs each one parsing it out into the vars seen below, name/carrer/code etc.
    For each of these 'NAMES' that we are looking for on the screen, grab the order# associated and add that to an array. Send that array to be processed (All works fine)
    Each iteration needs to clear out the arrayList so it can repopulated on the next go through. Line near the bottom (in red) is the line that refuses to work. I've tested this process separately and it works as intended. Example at bottom of post.

    Thoughts, anyone??

    Code:
    For Each rule In rulesByLine
    	autECLSession.autECLOIA.WaitForAppAvailable
    	autECLSession.autECLOIA.WaitForInputReady
    	autECLSession.autECLPS.autECLFieldList.Refresh
    	splitRule = split(rule)
    		name = Replace(splitRule(0), "_", " ")
    		curCarrier = Replace(splitRule(1), "_", " ")
    		newCARC = splitRule(2)
    		newSERV	= splitRule(3)
    		If Debug = true Then MsgBox("name = "&name&Chr(13)&"curCarrier = "&curCarrier&Chr(13)&"newCARC = "&newCARC&Chr(13)&"newSERV = "&newSERV&Chr(13))
    		
                    '--- Initializes, or reinitializes ordersToChange as an array list, needs to be empty each cycle.
    		Set ordersToChange = CreateObject("System.Collections.ArrayList")
    		
    	
    	Do While isNextPage = true
    		If Debug = true Then MsgBox("isNextPage is True")
    		autECLSession.autECLOIA.WaitForAppAvailable
    		autECLSession.autECLOIA.WaitForInputReady
    		autECLSession.autECLPS.autECLFieldList.Refresh
    		If Debug = true Then MsgBox("FieldList Refreshed")
    		
    		Set nameToFind = autECLSession.autECLPS.autECLFieldList.FindFieldByText(name, 1, 1, 1)
    		If nameToFind Is Nothing Then
    			If Debug = true Then MsgBox("nameToFind was NOTHING")
    			autECLSession.autECLPS.SendKeys "[roll up]"
    		Else
    			If Debug = true Then MsgBox("nameToFind was "& nameToFind.GetText)
    			Do While Not nameToFind Is Nothing
    				If Debug = true Then MsgBox("nameToFind was NOT Nothing: Found At "&nameToFind.StartRow&", "&nameToFind.StartCol)
    				If inStr(autECLSession.autECLPS.autECLFieldList.FindFieldByRowCol(nameToFind.StartRow, 46).GetText, curCarrier) Then
    					If Debug = true Then MsgBox("curCarrier Matched rule")
                                             '--- This is where numbers are added to the array only if they're found on screen (Works as designed)  
    					ordersToChange.Add autECLSession.autECLPS.GetText(nameToFind.StartRow, 6, 10)
    				End If
    				
    				Set nameToFind = autECLSession.autECLPS.autECLFieldList.FindFieldByText(name, 1, nameToFind.EndRow, nameToFind.EndCol)
    			Loop
    			autECLSession.autECLPS.SendKeys "[roll up]"
    		End If
    	Loop
    	
    	Dim text
    	If ordersToChange.Count > 0 Then
    		If Debug2 = true Then
    			For Each order in ordersToChange
    				text = text & order & Chr(13)
    			Next
    			MsgBox(text)
    		End If
    		changeCarrier ordersToChange, newCARC, newSERV		'--- If there is a list, sends list to cycle through each thing in the list and do stuff with it (All works as intended)
    		Set ordersToChange = nothing			'--- This is supposed to reset the list, ordersToChange.Clear won't work either. Instead, on the next go-around(s) it just adds more numbers to the array as if this line didn't exist..
    		autECLSession.autECLOIA.WaitForAppAvailable
    		autECLSession.autECLOIA.WaitForInputReady
    		navigateTo "OR3"           '--- I know this whole statement is being run properly as this command does what it's supposed to do. If it didn't, the whole thing would break after changeCarrier is called. changeCarrier is being called correctly during testing so I know this section of code is being called.
    	End If
    	autECLSession.autECLPS.SendKeys "[pf5]"
    Next
    Below was the test I ran to make sure this concept works and this worked exactly as designed. Set the array to nothing after having proved index 0 had text, and show the new text at the same index.
    If this works, why won't it work in the above code? My debug MsgBox does call and shows me exactly what's in the array and after finishing one round and moving on to another the array still has the original contents even after Set array = nothing is done...

    Code:
    Dim arrayName
    
    Set arrayName = CreateObject("System.Collections.ArrayList")
    arrayName.Add "This is just some text"
    
    MsgBox(arrayName(0))
    
    Set arrayName = nothing
    Set arrayName = CreateObject("System.Collections.ArrayList")
    
    arrayName.Add "This is now the new text"
    
    MsgBox(arrayName(0))
    I tried something. Better question.. Why are the 2 outputs of this code what you see below??
    Attachment 193954Attachment 193955
    I'm starting to think there's something fundamentally wrong with the arrayList objects themselves.
    by using .Clear and setting it to 'nothing' one of those should have worked to empty out the array to allow the second .Add to populate it with a single line. The second output should be showing only one line "This is now the new text".. But it shows both lines of text even though I've 'DUMPED' the arrayList.. What is going on here?

    Code:
    Dim arrayName, order, text
    
    Set arrayName = CreateObject("System.Collections.ArrayList")
    arrayName.Add "This is just some text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)
    
    arrayName.Clear
    Set arrayName = nothing
    Set arrayName = CreateObject("System.Collections.ArrayList")
    
    arrayName.Add "This is now the new text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)
    Last edited by BattiestGnat; Jan 22nd, 2025 at 05:02 PM.

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Is there a reason you are using CreateObject rather than just creating an instance of ArrayList?

    In fact I wouldn't use ArrayList anyway, if you are dealing with strings then something like List(Of String) would make more sense.

    However, you could just assign a new instance at the start of the For loop, no need to set it to nothing. You should be able to declare the variable inside the For loop, that way you wouldn't need to worry about clearing it.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    The reason I'm using CreateObject is whatever weird limitations this program has won't allow me to declare any type of array with an indeterminate number of indexes. Most of the arrays I use have lengths that will very each time I run the script. This is why I rely on using .Add to construct the array over time. This is the first time I've had the need to reuse the same array and is why I'm confused by how thing.Clear & Set thing = nothing refuse to work.
    I have yet to look into List, the limiter on that would be if there's no way to periodically add to that list as the for loop runs its course.. As well as being able to use For Each thing in List.

    I'm not entirely sure what you're referencing when you say "assign a new instance" Does this involve using a 'New' parameter to reset the object? Like listObject = New CreateObject("Systemmmm)? No idea how the syntax would work but is that what you're thinking? In theory, each time my For Each loop runs, it's calling
    Code:
    Set ordersToChange = CreateObject("System.Collections.ArrayList")
    Would this not already be a complete reset of ordersToChange even if it has content? Because I'm reusing the same arrayList I need it to empty out after each For Each iteration.
    I even moved the Set ordersToChange = nothing to the beginning of the whole For Each loop so I know it's running every time and that still didn't work. I'm also still confused how my test script worked exactly how I wanted it to and my actual script while functionally identical, won't...

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    *** Appended original post with this question ***
    I tried something. Better question.. Why are the 2 outputs of this code what you see below??
    Attachment 193954Attachment 193955
    I'm starting to think there's something fundamentally wrong with the arrayList objects themselves.
    by using .Clear and setting it to 'nothing' one of those should have worked to empty out the array to allow the second .Add to populate it with a single line. The second output should be showing only one line "This is now the new text".. But it shows both lines of text even though I've 'DUMPED' the arrayList.. What is going on here?

    Code:
    Dim arrayName, order, text
    
    Set arrayName = CreateObject("System.Collections.ArrayList")
    arrayName.Add "This is just some text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)
    
    arrayName.Clear
    Set arrayName = nothing
    Set arrayName = CreateObject("System.Collections.ArrayList")
    
    arrayName.Add "This is now the new text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)
    Last edited by BattiestGnat; Jan 22nd, 2025 at 05:01 PM.

  5. #5
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    You can just do...

    Code:
    ordersToChange = new ArrayList () 
    'or
    ordersToChange = new List(Of String) ()
    Also you don't need to use Set, and there is no need to set a variable to nothing if you are going to assign a new value to it.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    I attempted to use
    Code:
    ordersToChange = new ArrayList()
    It threw an error, 'Expected End of Statement'

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Where did you put that code? It needs to be within a sub or function, just the same as your existing code that is assigning something to that variable.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Oh??? This line is within the For Each loop which itself is NOT in any function/sub. I'll try making a sub clearOrderArray() to do this one task and see how it goes.

    *EDIT* Negative, same error even while wrapped in a sub
    Is there a way to declare this as an ArrayList() at the start?

    rather than Dim myList,

    using Dim myList As ArrayList() ??
    Last edited by BattiestGnat; Jan 22nd, 2025 at 06:18 PM.

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by BattiestGnat View Post
    Oh??? This line is within the For Each loop which itself is NOT in any function/sub. I'll try making a sub clearOrderArray() to do this one task and see how it goes.

    *EDIT* Negative, same error even while wrapped in a sub
    If it was inside the For loop then it should have worked, could you post the code you used when you got that error?

    Are you saying the For Loop isn't inside a sub or function? If so that will be the error.
    Last edited by PlausiblyDamp; Jan 22nd, 2025 at 06:24 PM.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    vvv- New Sub I just made, line calling this sub in bold.
    Still points to line 170 with the Expected End of statement

    Code:
    sub clearOrdersToChange()
    	ordersToChange = new ArrayList()  <---- Line 170
    end sub
    
    navigateTo "OR3"
    
    For Each rule In rulesByLine
    	autECLSession.autECLOIA.WaitForAppAvailable
    	autECLSession.autECLOIA.WaitForInputReady
    	autECLSession.autECLPS.autECLFieldList.Refresh
    	splitRule = split(rule)
    		name = Replace(splitRule(0), "_", " ")
    		curCarrier = Replace(splitRule(1), "_", " ")
    		newCARC = splitRule(2)
    		newSERV	= splitRule(3)
    		If Debug = true Then MsgBox("name = "&name&Chr(13)&"curCarrier = "&curCarrier&Chr(13)&"newCARC = "&newCARC&Chr(13)&"newSERV = "&newSERV&Chr(13))
    		
    		clearOrdersToChange        '<------------------ HERE
    		'Set ordersToChange = CreateObject("System.Collections.ArrayList")			'--- Initializes, or reinitializes ordersToChange as an array list, needs to be empty each cycle.
    		
    	
    	Do While isNextPage = true
    		If Debug = true Then MsgBox("isNextPage is True")
    		autECLSession.autECLOIA.WaitForAppAvailable
    		autECLSession.autECLOIA.WaitForInputReady
    		autECLSession.autECLPS.autECLFieldList.Refresh
    		If Debug = true Then MsgBox("FieldList Refreshed")
    		
    		Set nameToFind = autECLSession.autECLPS.autECLFieldList.FindFieldByText(name, 1, 1, 1)
    		If nameToFind Is Nothing Then
    			If Debug = true Then MsgBox("nameToFind was NOTHING")
    			autECLSession.autECLPS.SendKeys "[roll up]"
    		Else
    			If Debug = true Then MsgBox("nameToFind was "& nameToFind.GetText)
    			Do While Not nameToFind Is Nothing
    				If Debug = true Then MsgBox("nameToFind was NOT Nothing: Found At "&nameToFind.StartRow&", "&nameToFind.StartCol)
    				If inStr(autECLSession.autECLPS.autECLFieldList.FindFieldByRowCol(nameToFind.StartRow, 46).GetText, curCarrier) Then
    					If Debug = true Then MsgBox("curCarrier Matched rule")
    					ordersToChange.Add autECLSession.autECLPS.GetText(nameToFind.StartRow, 6, 10)
    				End If
    				
    				Set nameToFind = autECLSession.autECLPS.autECLFieldList.FindFieldByText(name, 1, nameToFind.EndRow, nameToFind.EndCol)
    			Loop
    			autECLSession.autECLPS.SendKeys "[roll up]"
    		End If
    	Loop
    	
    	Dim text
    	If ordersToChange.Count > 0 Then
    		If Debug2 = true Then
    			For Each order in ordersToChange
    				text = text & order & Chr(13)
    			Next
    			MsgBox(text)
    		End If
    		'changeCarrier ordersToChange, newCARC, newSERV		'--- If there is a list, sends list to cycle through each thing in the list and does stuff
    								'--- This is supposed to reset the list, ordersToChange.Clear won't work either.
    		autECLSession.autECLOIA.WaitForAppAvailable
    		autECLSession.autECLOIA.WaitForInputReady
    		navigateTo "OR3"
    	End If
    	autECLSession.autECLPS.SendKeys "[pf5]"
    Next

  11. #11
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    The For loop needs to be inside a method, you can't have code outside of a method.

    Unless you are trying to create a sub inside of an existing method, that might be an error in vb.

    If you go to the original code you posted, but get rid of the CreateObject calls and use the code I suggested what happens?

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    I have no reason to disagree as I know so little about this but I am perplexed as to why the code below runs just dandy with everything outside of subs/methods. Thoughts??

    Closing up shop at the warehouse, will play with things when I have more time tomorrow and I'll pick this convo back up then. Thanks for the help so far!!

    Code:
    [PCOMM SCRIPT HEADER]
    LANGUAGE=VBSCRIPT
    DESCRIPTION=
    [PCOMM SCRIPT SOURCE]
    OPTION EXPLICIT
    autECLSession.SetConnectionByName(ThisSessionName)
    
    Dim arrayName, order, text
    
    Set arrayName = CreateObject("System.Collections.ArrayList")
    arrayName.Add "This is just some text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)
    
    arrayName.Clear
    Set arrayName = nothing
    Set arrayName = CreateObject("System.Collections.ArrayList")
    
    arrayName.Add "This is now the new text"
    
    For Each order in arrayName
    	text = text & order & Chr(13)
    Next
    MsgBox(text)

  13. #13
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,815

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Possibly a question I should have asked earlier...

    What language is this written in? I just noticed in your first post you mentioned it was a macro for an AS400, do you know what language these are written in?

    I am starting to suspect it isn't vb.net

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,327

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Looks more like VB6.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    I'm not entirely sure, all I can tell is that some VBscript things work and some do not. This program is ancient so it's possible its some kind of derivative of VB.
    I've had a couple nights to think about it and I'm thinking I might go with an earlier idea I had that's a bit bloated code wise but should get me to the same outcome. If I can't figure out how to empty an existing array to reuse than I'll just let the script do what it wants to do and make one giant list instead cycling through multiple lists. Knowing how split() works I can just have the parameters passed alongside as instructions in string format. (I don't expect this to make complete sense as it's a workflow in my own head right now)

    TLDR; Rather than bashing my head against the wall wondering why a basic Method won't work, I'll use Methods/Techniques that I do know work to get what I need.

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,688

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    This certainly is NOT vb.NET. My guess is that it is some kind of VBScript, even though you say that some VBScript things do not work. The code, as written, wouldn't even compile in VB.NET, so that's out. I'm moving the thread to Classic VB in hopes that some of the older hands there know what this is.
    My usual boring signature: Nothing

  17. #17
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,441

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Since it is VBScript, execution is "top down" rather than by event driven methods, so having, at the very least, one line of code outside of methods is actually completely necessary.

    Glancing at your posts, it seems like "traditional" arrays that VBScript uses natively should work for your needs.

    Good luck.

  18. #18
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,441

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by Shaggy Hiker View Post
    This certainly is NOT vb.NET. My guess is that it is some kind of VBScript, even though you say that some VBScript things do not work. The code, as written, wouldn't even compile in VB.NET, so that's out. I'm moving the thread to Classic VB in hopes that some of the older hands there know what this is.
    The header of the code in post 12 states that it is VBScript.

  19. #19

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Sorry for the trouble, and thank you!

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,688

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Okay, I have now moved it to VBScript.
    My usual boring signature: Nothing

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,688

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by BattiestGnat View Post
    Sorry for the trouble, and thank you!

    These things happen with older code. Identifying the animal can be difficult.
    My usual boring signature: Nothing

  22. #22

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by OptionBase1 View Post
    Since it is VBScript, execution is "top down" rather than by event driven methods, so having, at the very least, one line of code outside of methods is actually completely necessary.

    Glancing at your posts, it seems like "traditional" arrays that VBScript uses natively should work for your needs.

    Good luck.
    Since you can't just .Add to traditional arrays I'll likely have to manage an index counter as the array is populated via a For loop adding one string at a time. Is that the idea?
    Also, to blow out the array for reuse, I'd just use ReDim myArray() to bring it back to empty?

  23. #23
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,441

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by BattiestGnat View Post
    Since you can't just .Add to traditional arrays I'll likely have to manage an index counter as the array is populated via a For loop adding one string at a time. Is that the idea?
    Also, to blow out the array for reuse, I'd just use ReDim myArray() to bring it back to empty?
    Correct. Admittedly it is more cumbersome than letting something like an ArrayList do the heavy lifting.

    https://learn.microsoft.com/en-us/pr...ectedfrom=MSDN

  24. #24

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    I appreciate all the help. I'll do a rebuild over the next few days and see where it takes me.

  25. #25

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    12

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    I figured it out... I still don't know what version of VB I'm using but I did get an arrayList to work and also used myArray.Clear and it worked.

    When I originally reached out asking why Setting = nothing nor array.Clear were working, they were in fact, working correctly. It was my debug code that wasn't working as I wasn't clearing the text used to compile the array's entries to something I can see... So each iteration was adding the new entries into the text string making it bigger and bigger. All I needed was one line of text = "" in there and I would have known there was no error to begin with.

    It's been 20 years since I've done anything more than a basic For Loop in any environment. I forgot just how stupid I can feel when these silly little mistakes cause hours of frustration.. I need a break, it's lunch time. >_<

    TLDR; Code in question was working correctly the entire time, it was my debug code to 'visualize' what the script is doing that wasn't accurately representing what was happening..

  26. #26
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,441

    Re: Contents of an arrayList persist through all attempts to dump. WHY?

    Quote Originally Posted by BattiestGnat View Post
    I figured it out... I still don't know what version of VB I'm using but I did get an arrayList to work and also used myArray.Clear and it worked.

    When I originally reached out asking why Setting = nothing nor array.Clear were working, they were in fact, working correctly. It was my debug code that wasn't working as I wasn't clearing the text used to compile the array's entries to something I can see... So each iteration was adding the new entries into the text string making it bigger and bigger. All I needed was one line of text = "" in there and I would have known there was no error to begin with.

    It's been 20 years since I've done anything more than a basic For Loop in any environment. I forgot just how stupid I can feel when these silly little mistakes cause hours of frustration.. I need a break, it's lunch time. >_<

    TLDR; Code in question was working correctly the entire time, it was my debug code to 'visualize' what the script is doing that wasn't accurately representing what was happening..
    Nice!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width