Only since the bugs relating to looking-up duplicate variable names seem to be corrected now in the last FB version 1.09.0 !
(otherwise, big problems before)
Hi, the simplest way to share variables within a SUB or FUNCTION is to use global variables with: DIM SHARED myvar AS "TYPE"Mike Green9 wrote: ↑Feb 09, 2022 18:46 FreeBasic 1.07 Ubuntu 20.04
Hi.
Here is a sample program containing a Function GetWeight(). Is there anyway that a Function can access ALL program Variables such as Var1, Var2, and String1 in this example ?
I would also ask the same question about Sub/End Sub.Code: Select all
Dim As Integer Var1, Var2, Weight, W Dim As String String1, String2 Public Function GetWeight() As Integer Dim As Integer Weight = 20 Var1 = Weight * Var2 'Defined Outside Of This Function String1 = "Changed in Function" ''Defined Outside Of This Function Return Weight End Function ' Program Begins Var1 = 87 : Var2 = 12 : Weight = 11 String1 = "String1" : String2 = "String2" W = GetWeight Print W, Var1, Var2, String1, String2
Thanks,
M....
Code: Select all
Dim shared as integer Var1 , Var2
Dim shared as string String1
Function GetWeight() As Integer
Dim As Integer Weight = 20
Var1 = Weight * Var2 'Defined Outside Of This Function
String1 = "Changed in Function" ''Defined Outside Of This Function
Return Weight
End Function
' Program Begins
dim as integer Weight = 20
Var1 = 87 : Var2 = 12
String1 = "String1" : String2 = "String2"
W = GetWeight(Weight)
Print W, Var1, Var2, String1, String2
Code: Select all
type myclass
Dim as integer Var1 , Var2
Dim as string String1, String2
Public:
Declare Function GetWeight(byval Weight as integer ) As Integer
Private:
dim as integer privatevar = 10 ' this var can be used only by member of this class
end type
Function myclass.GetWeight(byval Weight as integer) As Integer ' This function preceded by the name of the class. becomes an effective member of the class
this.Var1 = Weight * this.Var2 'Defined Outside Of This Function
this.String1 = "Changed in Function" 'Defined Outside Of This Function
'this.privatevar you can use here
Return Weight
End Function
' Program Begins
DIM example as myclass 'We create an object by defining it as a class
dim as integer Weight = 20
dim as integer W = 0 ' You have to define every var or array you need to be used
example.Var1 = 87 : example.Var2 = 12
example.String1 = "String1" : example.String2 = "String2"
W = example.GetWeight(Weight) 'Weight is not a public or shared variable therefore if it is to be read within the function it must be specified in the function itself. (byval myvar as type)
Print W, example.Var1, example.Var2, example.String1, example.String2
'example.privatevar you cannot use here because is private
Nope
Code: Select all
dim shared as integer SharedVar = 1
dim as integer NoSharedVar = 1
sub mysub()
print SharedVar 'show 1'
print NoSharedVar 'error
end sub
print SharedVar 'show 1'
print NoSharedVar 'show 1'
mysub()
Code: Select all
namespace global
dim as integer SharedVar = 1
end namespace
dim as integer NoSharedVar = 1
sub mysub()
print global.SharedVar 'show 1'
print NoSharedVar 'error
end sub
print global.SharedVar 'show 1'
print NoSharedVar 'show 1'
mysub()
Sometime you have no choice:Lost Zergling wrote: ↑Jun 18, 2022 0:15 Usually most skilled programmers will consider as a good practise to use as less global declaration as possible, nevertheless :
Code: Select all
dim shared CurrentUser as string
sub SetUser()
CurrentUser = "USER"
end sub
sub testA()
print "The current user:" & CurrentUser
end sub
Sub Directory()
open for input CurrentUser & "\mysave.sav" as #1
close #1
end sub
Yes you are right, use a Global like a static its useless and can cause some troubles , some time you have to change data inside a global and then istead static you ave to use a global var.