How do I define FileOpen(C1 the Connection Name Globally

apstrojny2

Member
Licensed User
Longtime User
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


Thanks Andy
 

specci48

Well-Known Member
Licensed User
Longtime User
Hello apstrojny2,

just put dim c1 into the globals definition:
B4X:
Dim c1

So this code works:
B4X:
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


specci48
 

derez

Expert
Licensed User
Longtime User
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
 

specci48

Well-Known Member
Licensed User
Longtime User
The connection is global "by definition",you don't need to declare it in globals.
Surprising information since this is not mentioned in the help file!

Anyway I find it useful treating connections as all other "normal" variables and define a global dim statement for this.


specci48
 

agraham

Expert
Licensed User
Longtime User
treating connections as all other "normal" variables and define a global dim statement for this.
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.
 

specci48

Well-Known Member
Licensed User
Longtime User
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.

Thanks for clearifying this.
Even it is possible it really would confuse me using the same name for different purposes at the same time...


specci48
 

Cableguy

Expert
Licensed User
Longtime User
Me too, some of us are human - although my better half does wonder at times!

By the way, how did the memory upgrade go?!?! :sign0188::sign0142:
 

apstrojny2

Member
Licensed User
Longtime User
Thanks specci48, derez, and agraham....

The example was excellent and helped a lot, you folks really know this stuff... I got that part of my program working...

I do have another interesting network stream question... I'll start a new thread...

Best regards,

Andy
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…