Append to file and global variables

jschuchert

Active Member
Licensed User
Longtime User
I added a variable named 'strfilename' as a process global variable. In one of my activity modules (Start) I use it as follows:
B4X:
Sub btnOK_click
strfilename=txtFileName.text
File.OpenOutput (File.DirInternal ,strfilename,True) 'should append data
End Sub

Now in another module (Input) I used it again like this:
B4X:
Sub btnSave_click
coord(0)=txtPoint.Text   (This is repeated with new values multiple times)
coord(1)=txtNorth.Text 
coord(2)=txtEast.Text 
coord(3)=txtDesc.Text 

File.OpenOutput (File.DirInternal,strfilename,True) 'shouldn't have to open again since it was opened in the start module
File.WriteString (File.DirInternal ,strfilename,coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3) & CRLF)
End Sub

The first code seems to accept strfilename. The second does not but complains that strfilename is an undeclared variable. I thought that it was declared in the Start module. In order to test, I had to give the file a literal name (jimtest). Even then I cannot get it to append the data. It only displays the last entry when I use the following code in another module (embed). The code itself my be flawed.
B4X:
txtdisplay.Text=File.GetText (File.DirInternal ,"jimtest")
OR
txtdisplay.text=File.ReadString(File.DirInternal,"jimtest")

There must be something I am not seeing about global variables because I have a similar problem when I declare something like coord(4) or use a type like
Type coord(point as string, north as double, east as double,desc as string)
Dim point, north,east,desc

In my app coord(4) means 4 elements like coord(0), coord(1), etc. It doesn't mean that a variable named 'coord' has 4 items. Maybe that is not correct. My app will use globals between modules quite extensively so it is critical I fully understand what is happening.

I have tons of global variables used in my PPC version that I have transferred to b4A but won't know if they work as I anticipate until I actually use them.

It is also important that I am able to append data to my files. I also experimented with other scenarios in addition to the code above, thinking each time that I had the solution but did not. I essentially followed what I did in b4ppc but it had a file.close command.

I am not very good at explaining things so please ask questions if you are not clear on what I am talking about. Thanks for your willingness to help.

Jim
 

kickaha

Well-Known Member
Licensed User
Longtime User
Lets see if I can shed some light:

The global variables - if you declare a process global variable in an activity, to use it in another activity you have to reference its parent.

So if you create txtFileName in your main activity, to use it in embed (for example), the name is main.txtFileName (when you type main. you will get a list of available vars).

File append problem - File.WriteString writes the string to a NEW file (so it will replace your old one).
You need something like:
B4X:
Sub btnSave_click
coord(0)=txtPoint.Text   (This is repeated with new values multiple times)
coord(1)=txtNorth.Text 
coord(2)=txtEast.Text 
coord(3)=txtDesc.Text 

Dim Writer As TextWriter

Writer.Initialize (File.OpenOutput (File.DirInternal,strfilename,True))
Writer.Write (coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3) & CRLF)
Writer.Close
End Sub

The other problem - not sure I follow you here, but are you trying to define and use a type type called coord? if so:
B4X:
Type coord (point as string, north as double, east as double,desc as string)
Dim coord As coord ' you now have a variable called coord of type coord

' you can then address the elements like this

coord.point = "test"
coord.north = 1234
coord.east = 5678
coord.desc = "another test"

' even better, if this is declared and Dim ed in Process_Globals you can use it
' in other modules - so if it was declared in main then
' main.coord.point etc works.

If I got all three right I am demanding "expert" status off Erel ;)

edit to add:
When you declare a type you do not need to dim the individual elements as
B4X:
Dim point, north,east,desc
 
Last edited:
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Andy,

How do you live with yourself, being so clever? If it is post count that moves you up the ladder, I'll do my best to get you over the top.

Now I remember the 'parent-variable' thing (I believe from NSBasic). It makes sense and i should have remembered. My desktop version was written in VB6 a long time ago and coord.pointno, coord.north, etc. is what I used. How soon we forget!

Although I haven't yet tried your code and suggestions I am confident everything is just right and will do the job perfectly and make my life easier. Your prompt responses have certainly been welcome and mere words cannot adequately express my appreciation. I will try to not bother you over the weekend so have a good one.

Where are you located? I am in Utah.

Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Jim,

Post up whenever you need to, I read and respond when I want so you would not be "disturbing me.

I am originally from England but now live in Wales (part of Great Britain). Getting some Archery in over weekend, so should be fun.
 
Upvote 0
Top