-
String array problem
Hi,
I have an app which searches for all the MS Agent Characters on the pc and stores their names in an array, so I can load them later.
I have this code to add them to the array but it's not working:
The array AgentNames has been declared in General Declarations
Code:
Dim FSO As FileSystemObject
Dim FSOFolder As Folder
Dim FSOFile As File
Dim sPath As String
On Local Error Resume Next
Set FSO = New FileSystemObject
' Load Characters into array
sPath = FSO.GetSpecialFolder(WindowsFolder) & "\MSAgent\Chars\"
If FSO.FolderExists(sPath) Then
Set FSOFolder = FSO.GetFolder(sPath)
lCount = 0
For Each FSOFile In FSOFolder.Files
ReDim Preserve AgentNames(UBound(AgentNames) + 1) As String
AgentNames(lCount) = FSOFile.Name
lCount = lCount + 1
Next
Else
MsgBox "Error while loading characters!", vbOKOnly + vbCritical, "Error"
Unload Me
End If
When I want to scroll through the names with 'AgentNames(scrollindex), it gives an error.
Does anyone know what I should do?
Tnx in advance,
MK
-
where exactly are you getting the error and what error is it?
-
I use this to scroll through the array:
CharScroll is a public Int
Code:
If CharScroll < lCount Then
CharScroll = CharScroll + 1
Else
CharScroll = 0
End If
Then I use this code to load the Agent:
Code:
Set Assistant = Nothing
CoreAgent.Characters.Unload ("Assistant")
CoreAgent.Characters.Load "Assistant", AgentNames(CharScroll)
Set Assistant = CoreAgent.Characters("Assistant")
Assistant.LanguageID = &H409
Assistant.Show
Then at the line 'CoreAgent.Characters.Load "Assistant",AgentNames(CharScroll) VB gives the error: "Subscript out of range"
What am I doing wrong?
-
Make Sure CharScroll is not going above the max of AgentNames.
One way to tell what the max of AgentNames is is to print it's ubound. "Debug.Print Ubound(AgentNames) & " - " & CharScroll"
Make sure that CharScroll never goes above the Ubound(AgentNames)
-
It's not possible for CharScroll to exceed the max of AgentNames.
The max of AgentNames = LCount and CharScroll cannot exceed LCount.
And the error already occurs after I've scrolled 1 time.
In Form_Load I set ScrollChar to 0, so the error already occurs when ScrollChar is 1, and LCount is 2(On my PC)
So i think the problem has something to do with the declaration of my Array, but I cannot find anything!
But tnx for your time and efforts!
-
Do you have
Dim Variable (0 to [upperbound]) as sometype
??
Or simply
Dim Variable as sometype
or
Dim Variable() as sometype
??
-
I've declared in General Declarations as:
Dim AgentNames() as string
Since I don't know how much MS Agent Characters there are installed on the PC when my app loads, I cannot give it a length yet..........
-
You still need to give it a length, then use another variable to track how many indexes you are using.
I usually end up creating a TYPE for these sort of cases.
Public Type MyType
indexes as integer
myarray(1 to 255) as integer
end type
dim myvar as mytype