How do I define variable C1 the 'Connection Name' Globally... This way I can test for existence in one Sub, open it in another Sub, use it in another Sub and close it in a Fourth...
I know something needs to go in Sub Globals but I sure don't know what...
If FileExist ("Data.txt") = true Then
FileOpen (c1,"Data.txt",cRead ,, cASCII)
r = FileRead (c1)
Do Until r = EOF
sum = sum + r
r = FileRead (c1)
Loop
Msgbox (sum)
FileClose (c1)
End if
Sub Globals
Dim c1
End Sub
Sub App_Start
Form1.Show
TestSub1
End Sub
Sub TestSub1
If FileExist ("Data.txt") = True Then
FileOpen (c1,"Data.txt",cRead ,, cASCII)
Test2
FileClose (c1)
End If
End Sub
Sub TestSub2
r = FileRead (c1)
Do Until r = EOF
sum = sum + r
r = FileRead (c1)
Loop
Msgbox (sum)
End Sub
The connection is global "by definition",you don't need to declare it in globals. You can use it in several subs and it always refer to the same connection.
I checked it with this example:
B4X:
Sub Globals
'Declare the global variables here.
count = 0
End Sub
Sub App_Start
Form1.Show
FileOpen(c1,"testfile.txt",cWrite)
FileWrite(c1,"Start")
End Sub
Sub Button1_Click
count = count + 1
FileWrite(c1,count)
End Sub
Sub Button2_Click
FileClose(c1)
End Sub
Actually you are not defining the connection at all, you are just defining a normal global variable that has the same name as a connection.
Connections are treated differently to and indepenently of variables and can have the same name as a variable without problem. Try it - you can assign to and use a variable with the same name as an open connection and nothing bad happens.
Connections are treated differently to and indepenently of variables and can have the same name as a variable without problem. Try it - you can assign to and use a variable with the same name as an open connection and nothing bad happens.