Syntax error

parijs

Active Member
Licensed User
Longtime User
I have files favo1 .... 10.txt
And I want to read a file by number


num=5

= File.ReadString(File.DirRootExternal, "Mymap/settings/favo"&(num)".txt")

I've tried different things, but get syntax or file not exist.
 
excuus me :BangHead:

How thas it work by Dim

I have dim favo1.....10 as String

num=5

favo(?)=

where can I learn this simple things
 
Upvote 0
Rather than have 10 individually declared strings you should use an array. So let's say you currently have this:
Code: Copy
Dim favo1 As String
Dim favo2 As String
Dim favo3 As String
Dim favo4 As String
Dim favo5 As String
Dim favo6 As String
Dim favo7 As String
Dim favo8 As String
Dim favo9 As String
Dim favo10 As String
You can replace that with:
Code: Copy
Dim favo(10) As String
Which is essentially doing the same thing but leaving you with much easier to work with variables.

Now to populate the array you can use a loop. However, arrays start at 0 - not 1. You can fix this using two ways. Either rename your files to favo0.txt, favo1.txt... favo9.txt (this is definitely the route I would recommend) or add 1 to the current index in the loop:
Code: Copy
For i = 0 to 9
   favo(i) = File.ReadString(File.DirRootExternal, "Mymap/settings/favo" & i+1 & ".txt")
Next
This will leave you with:
Code: Copy
favo(0) = (Contents of favo1.txt)
favo(1) = (Contents of favo2.txt)
favo(2) = (Contents of favo3.txt)
favo(3) = (Contents of favo4.txt)
favo(4) = (Contents of favo5.txt)
favo(5) = (Contents of favo6.txt)
favo(6) = (Contents of favo7.txt)
favo(7) = (Contents of favo8.txt)
favo(8) = (Contents of favo9.txt)
favo(9) = (Contents of favo10.txt)
 
Last edited:
Upvote 0
Hi admac,

this code give me

Dim favo(10) As String


For i = 1 To 9

If File.Exists(File.DirRootExternal, "VisitParijs/settings/favo"&i&".txt") = True Then
favo(i) = File.ReadString(File.DirRootExternal, "VisitParijs/settings/favo"&i&".txt")
End If
Next


Error description: Current declaration does not match previous one.
Previous: {Type=favo,Rank=0}
Current: {Type=String,Rank=1}
Occurred on line: 351
Dim favo(10) As String
 
Upvote 0
Yes a have :sign0013:

Now I have favo () can I use it in

If Value = "favo(1)" Then
StartActivity(project)
End If

Thits not working
 
Last edited:
Upvote 0
Thanks Klaus,

But this code:

Dim favo(30) As String

Sub ListView1_ItemClick (Position As Int, Value As Object)
If Value = favo(1) Then
StartActivity(project)
End If

gives me this error

Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'favo' is used before it was assigned any value.
Occurred on line: 75
If Value = favo(1) Then
 
Upvote 0
If you want to use a variable throughout a module you should put it in Sub Globals otherwise it is only accessible by the subroutine that created it i.e
Code: Copy
Sub mySubroutine
Dim favo(10) As String
For i = 0 To 9
   favo(i) = File.ReadString(File.DirRootExternal, "Mymap/settings/favo" & i+1 & ".txt")
Next
Log(favo(1))
End Sub
will work fine because all declarations are within the same subroutine whereas
Code: Copy
Sub mySubroutine
Dim favo(10) As String
For i = 0 To 9
   favo(i) = File.ReadString(File.DirRootExternal, "Mymap/settings/favo" & i+1 & ".txt")
Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
If Value = favo(1) Then 
   StartActivity(project) 
End If
End Sub
will not work because favo is declared as a local variable in mySubroutine so can't be accessed by any other subroutine.

The solution (not the only one or the best but the easiest to understand) is to declare favo as a global variable:
Code: Copy
Sub Globals
Dim favo(30) As String
End Sub

Sub mySubroutine
Dim favo(10) As String
For i = 0 To 9
   favo(i) = File.ReadString(File.DirRootExternal, "Mymap/settings/favo" & i+1 & ".txt")
Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
If Value = favo(1) Then 
   StartActivity(project) 
End If
End Sub
 
Upvote 0
Can you please explain what you want to do because with just your small code snippets we need to try to find it out.
I'm afraid that you are mixing up variables and the values of variables.

In your case you Dim favo(30) as String.
That means that you have defined 30 String variables with the same name but with an index going from 0 to 29. All these variables are EMPTY.
So before using them you must assign them values.
Like:
Code: Copy
favo(0) = "gvvjjb"
favo(1) = "djdlk"
favo(2) = "bdbfkwn"
'etc
then you can use favo(0), favo(1) etc.

or
Code: Copy
For i = 0 To 29
    favo(i) = "Name" & i
Next
Then you have :
favo(0) = "Name0"
favo(1) = "Name1"
etc.

Where did you declare Dim favo(30) ?
- In Sub Process_Globals, the variables are accessible everywhere in the project
- In Sub Globals, the variables are accessible only in the Activity where they are declared.
- In any Sub, the variables are accessible insides the Sub where they are declared.

Did you have a look at the Documentation Wiki and the Beginner's Guide ?

Best regards.
 
Upvote 0
Back
Top