Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TYPE and CONST within SUB/FUNCTION
#10
Playing around with Types defined in Subs and Terry's test code, I switched to testing Type values and Shared with Main:
Code: (Select All)

Option _Explicit

Const SUB1_CONSTANT = 300 '      global everywhere unless also created within a subroutine
Const SUB2_CONSTANT = 400

'TYPE Type_SUB1 '                if these lines are enabled an error occurs in SUB1()
'    a AS INTEGER
'    b AS INTEGER
'END TYPE

Dim Sub1_Variable As Type_SUB1 ' Type_SUB1 is seen globally even though created in SUB1()
Dim Sub2_Variable As Type_SUB2 ' Type_SUB2 is seen globally as well even though created in SUB2()

SUB1
SUB2

Print "-----------------"
Print "-- IN MAIN CODE -"
Print "-----------------"
Print "Sub1_Variable.a  ="; Sub1_Variable.a
Print "Sub1_Variable.b  ="; Sub1_Variable.b
Print

'----------------------------------------------------

Sub SUB1 ()

    Const SUB1_CONSTANT = 100 '      constant is only local to this subroutine now

    Type Type_SUB1
        a As Integer
        b As Integer
    End Type

    Shared Sub1_Variable As Type_SUB1 ' completely unique variable from one created at main code level
    Sub1_Variable.a = 1
    Sub1_Variable.b = 2
    Print "-----------------"
    Print "---- IN SUB1 ----"
    Print "-----------------"
    Print "SUB1_Variable.a  ="; Sub1_Variable.a
    Print "SUB1_Variable.b  ="; Sub1_Variable.b
    Print

End Sub

'----------------------------------------------------

Sub SUB2 ()

    Const SUB2_CONSTANT = 200 '      constant is only local to this subroutine now

    Type Type_SUB2
        a As Integer
        b As Integer
    End Type

    Dim Sub2_Variable As Type_SUB1 ' completely unique variable from one created at main code level
    Sub2_Variable.a = 3
    Sub2_Variable.b = 4

    Print "-----------------"
    Print "---- IN SUB2 ----"
    Print "-----------------"
    Print "Sub2_Variable.a = "; Sub2_Variable.a
    Print "Sub2_Variable.b = "; Sub2_Variable.b

End Sub

Works as one might expect.
b = b + ...
Reply


Messages In This Thread
RE: TYPE and CONST within SUB/FUNCTION - by bplus - 07-10-2023, 12:30 AM
RE: TYPE and CONST within SUB/FUNCTION - by bplus - 07-10-2023, 12:55 AM
RE: TYPE and CONST within SUB/FUNCTION - by bplus - 07-11-2023, 01:22 AM



Users browsing this thread: 3 Guest(s)