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.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function Pointers? BlameTroi 5 230 02-20-2026, 05:55 PM
Last Post: BlameTroi
  QB64PE Excel-type spreadsheet supporting formulas and QB64PE macros? madscijr 33 1,448 01-30-2026, 12:28 AM
Last Post: madscijr
  Arbitrary CONST values Unseen Machine 31 2,878 10-29-2025, 05:44 AM
Last Post: bplus
  Variable length type declarations dano 5 688 08-06-2025, 09:53 PM
Last Post: dano
  Upcoming changes to CONST may affect your code SMcNeill 14 2,743 12-30-2023, 05:13 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)