Having problem with calling sub and using public variable

jschuchert

Active Member
Licensed User
Longtime User
I have named 'strfilename' as a public variable in the main module. That variable contains the name of the file I will be using within my app.
I have created a second module named ModPPC1
I added a sub named 'startinverse8' to the second module
Within startinverse8, I want to open 'strfilename' and do things.
Here are some snippets of code:

B4X:
Sub Globals
   'Declare the global variables here.
    Public strfilename [color=green]'declaration of public for variable[/color]
    Public strfilelist 
    Public coord(4)
    Public strbearing,strbear,strfrpoint,strdesc,strpointno,strdes
    Public intcounter,strdistance
    
   radian=0.017453292
   radianreverse=1/radian
   numer=""

End Sub

Sub App_Start
If Not (FileExist ("filelist.txt")) Then
FileOpen (f,"filelist.txt",cWrite,,cASCII)


B4X:
'this is where a file is assigned to strfilename
Sub lstStart_SelectionChanged (Index, Value)
txtstartfilename.Text =value
[color=red]strfilename=txtstartfilename.Text & ".txt"[/color]
FileOpen(f3,strfilename,cWrite,cAppend )
Msgbox(strfilename & " has been opened for cogo operations")
FileClose(f3)
'frmstart.close 
frmmenu.Show 
End Sub



B4X:
'from the calling sub in the main module
strBearing = sPoint1 & "*" & sPoint2
[color=red]CallSub ("modppc1.startinverse8",strbearing,strfilename)[/color]
end if
end sub

B4X:
'from the sub in modppc1
Sub startinverse8(strbearing,[color=red]strfilename[/color])
FileClose(c)
FileOpen(c, strfilename,cRead)
pintcntr = StrIndexOf(strBearing, "*",0)
firstpt = SubString(strBearing, 0, pintcntr)

I have to use the code noted above to make it work. 'strbearing' is a required parameter but why do I have to use 'strfilename' if I already declared it as public? If I don't, I get an error message saying "strfilename is not assigned any value" although earlier in my code I made sure an actual file name was assigned.

Also, if I had declared the sub 'startinverse8' as public, could I then just name it without using 'callsub' ? I have read the docs but maybe don't fully understand them the way I should. Thanks, guys, for any help or further explanation.

Jim Schuchert
 

agraham

Expert
Licensed User
Longtime User
why do I have to use 'strfilename' if I already declared it as public?
Public makes it visible outside of the module but to access it you need to qualify it with the module name as you have found. This means that several modules, perhaps written by different people, can use the same public names without conflict. Within the module where it is declared public you don't need the qualification as if a module name qualifier is not specified Basic4ppc internally uses the current module name.
 

jschuchert

Active Member
Licensed User
Longtime User
Thank you Agraham. So if this 75 year old brain understands correctly, if I declare a variable as public in any module, I have to reference that module if using the variable in a different module?

For example, in my main module, I declare "north" as a public variable. I create Module1 and add many subs. One of those is "Sub A". If I want to use the variable 'north' in that sub, do I reference it as 'main.north' or if it is declared in module1 and I use it in code in the main module, do I use "module1.north"? Of course I will comply with the rules but it is a departure from what I am used to doing. Since most of my variables are used interchangably with many subs would you recommend declaring them as 'Public' and putting them all in the same module and which one? I will only have main and 1 other module.

Thank you for your expertise.

Jim
 

agraham

Expert
Licensed User
Longtime User
if I declare a variable as public in any module, I have to reference that module if using the variable in a different module?
Yes.
One of those is "Sub A". If I want to use the variable 'north' in that sub, do I reference it as 'main.north'
If you use a variable (or Sub) in the module in which it is declared the module qualification is optional. You can use either syntax.
Of course I will comply with the rules but it is a departure from what I am used to doing.
It's sort of borrowed from object oriented languages and it's done for encapsulation to make each module independent and allow unambigous reuse of names.
Since most of my variables are used interchangably with many subs would you recommend declaring them as 'Public' and putting them all in the same module and which one?
You only need to make public the ones that are needed to be accessed from another module. Where you put them is a matter for your own style.
 

jschuchert

Active Member
Licensed User
Longtime User
Thank you very much, aGraham and Ariel, especially for the tutorial link. I understand it clearly now and believe it will make things much nicer for my coding. I am very pleased so far with B4P with all of its features, particularly for the ability to break the code and hover the mouse for variable values. VB6also does this and it is very useful in debugging. I also appreciate the detailed help system and this great forum with experts like you. Erel and his team (might include both of you) have done a super job and the cost is very affordable. Keep up the good work and I'm sure you will be hearing from me again.

Jim
 
Top