Announcement

Collapse
No announcement yet.

DIM ALL question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    DIM ALL question

    I DIM the variable temp$, but use temp in the code - yet the compiler doesn't complain. Is that the way it's supposed to be?

    Code:
    #Compile Exe
    #Dim All
    #Include "win32api.inc"
    
    Function PBMain() As Long
        Local hDlg As Dword
        Dialog New Pixels, 0, "Button Test",300,300,200,200, %WS_OverlappedWindow To hDlg
        Control Add Button, hDlg, 100,"Push", 50,10,100,20
        Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Dim temp$
       temp = "s"
       If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
       'test code
       End If
    End Function

    #2
    That is correct. The $ told the compiler that it is a type of STRING (could also use AS STRING), so you don't need the identifier to refer to it again once it is declared.
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


      #3
      Well, that's a surprise because Help says:

      Bear in mind that cat?, cat%, cat&, cat&&, cat!, cat#, cat##, cat@, cat@@, and cat$ are ten separate variables. Although using cat over and over again to create different variables like this is legal, good programming practice suggests that you use somewhat different names for different variables.
      But when I use

      Code:
      Dim a$, a%, a!
      the compiler complains.

      If it didn't, which a$, a%, a! would just plain old "a" be?

      There's an inconsistency in here somewhere.

      But you're right - with a single a% (or whatever type I give it), using plain old "a" compiles fine with #DIM ALL in it.

      Comment


        #4
        And then there's this data point too. I create a Global variable a% . Then I create a local variable a!.

        If I try to use a% in the procedure where a! was defined, the compiler complains.

        I guess the moral of the story is don't use variables with the same base name.

        Code:
        #Compile Exe
        #Dim All
        #Include "win32api.inc"
        
        Global a%
        Function PBMain() As Long
            a% = 10
            Local hDlg As Dword
            Dialog New Pixels, 0, "Button Test",300,300,200,200, %WS_OverlappedWindow To hDlg
            Control Add Button, hDlg, 100,"Push", 50,10,100,20
            Dialog Show Modal hDlg Call DlgProc
        End Function
        
        CallBack Function DlgProc() As Long
           If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
              Dim a!
              a% = 5
              MsgBox Str$(a)
           'test code
           End If
        End Functio

        Comment


          #5
          You can't use the same name even with different identifiers when declaring them. With no DIM ALL statement you can simply use them and the compiler won't complain.

          eg.

          Code:
          #Compile Exe
          
          Function PBMain
          
             
             A$ = "Hello"
             A% = 5
             
             ? A$
          
          End Function
          Scott Slater
          Summit Computer Networks, Inc.
          www.summitcn.com

          Comment


            #6
            Originally posted by Gary Beene View Post
            And then there's this data point too. I create a Global variable a% . Then I create a local variable a!.
            Local variables will be used instead of the globals when the names are the same and they are declared.

            The safest bet is to use UNIQUE names, and to always use #DIM ALL
            Scott Slater
            Summit Computer Networks, Inc.
            www.summitcn.com

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎