Variable not defined?!?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Darren123
    New Member
    • Jan 2008
    • 5

    Variable not defined?!?

    Hey, I'm using Vb6 and I have a problem with my code but I don't know why.
    Here it is:

    Code:
    Option Explicit
    
    'Opacity -------------------------
    Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    
    Private Const GWL_EXSTYLE = (-20)
    Private Const WS_EX_LAYERED = &H80000
    Private Const LWA_ALPHA = &H2&
    '--------------------------------------
    
    Private Function Opacity(Value As Byte, Frm As Form)
    On Error GoTo ErrorHandler
    
    Dim MaxVal As Byte, MinVal As Byte
        
    MinVal = 20: MaxVal = 255
        
    If Value > MaxVal Then Value = MaxVal
    If Value < MinVal Then Value = MinVal
        
    SetWindowLongA Frm.hwnd, GWL_EXSTYLE, GetWindowLongA(Frm.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    SetLayeredWindowAttributes Frm.hwnd, 0, Value, LWA_ALPHA
        
    ErrorHandler:   Exit Function
    End Function
    
    Private Sub hsbOpacity_Change()
    Opacity hsbOpacity.Value, Me
    End Sub
    
    Private Sub Text1_Change()
    n = Text1
    For i = 0 To n - 1
    NewStr = NewStr + Text2.Text + ";"
    Next i
    Text3 = NewStr
    End Sub
    The problem is when I run the program it says "Variable not defined" and highlights "n =" under "Private Sub Text1_Change()" but when I test this part of the code on it's own, it works fine, the other part of the code is for the opacity of the form and this works ok too.
    When I put the code together I get the problem, Anyone know a solution?

    Thanks, Darren.
  • Darren123
    New Member
    • Jan 2008
    • 5

    #2
    I was looking at random code and it gave me an idea, so I tried:

    Code:
    Private Sub Text1_Change()
    Dim n As Variant
    Dim i As Variant
    Dim NewStr As String
    n = Text1
    For i = 0 To n - 1
    NewStr = NewStr + Text2.Text + ";"
    Next i
    Text3 = NewStr
    End Sub
    And it has worked.

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by Darren123
      Hey, I'm using Vb6 and I have a problem with my code but I don't know why.
      (...)

      The problem is when I run the program it says "Variable not defined" and highlights "n =" under "Private Sub Text1_Change()" but when I test this part of the code on it's own, it works fine, the other part of the code is for the opacity of the form and this works ok too.
      When I put the code together I get the problem, Anyone know a solution?

      Thanks, Darren.
      sure, when you write Option Explicit, it forces you to declare any variable used in the code. when you run only a part, the 'option explicit' isnt activated so you dont have to define 'n'. The way to solve it is:
      Code:
      Remove the line 'Option Explicit' (i dont recomend you doing this, since i think using option explicit is a good habit), or
      Declare 'n':  Dim n As Long  - that will do (yeah!, that's better).
      Please check in your VB help file the types of variables to see which one fits your code better.

      HTH

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Yes, you should always use Option Explicit. It prevents a lot of weird problems that can be very difficult to track down. Because when you mistype a variable name, this causes VB to stop and tell you. without this option turned on, VB will just create a new variable with the new name, and continue on.

        Think about this. Let's assume that for some reason, you create a variable called nnnnnnnn. Then later, somewhere else in your code, you see this line...[CODE=vb]For Counter = 1 To nnnnnnn[/CODE]Unless you've got very sharp eyes, you won't spot any difference (trust me, there is one). If you have set Option Explicit for this module, VB will refuse to compile, because the variable nnnnnnn doesn't exist. If you don't, it will create a new local variable at that point. The new variable will have value 0, so the loop will never execute. You can waste days, desperately trying to figure out why your FOR loop never accomplishes anything.


        kadghar, I have to take exception to your statement "when you run only a part, the 'option explicit' isnt[sic] activated". Although I can't guarantee it 100%, I'm pretty sure this is not true. If the option is there, it takes effect for that module.

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by Killer42
          ...
          kadghar, I have to take exception to your statement "when you run only a part, the 'option explicit' isnt[sic] activated". Although I can't guarantee it 100%, I'm pretty sure this is not true. If the option is there, it takes effect for that module.
          Yes, sorry about that.

          Comment

          • Darren123
            New Member
            • Jan 2008
            • 5

            #6
            That's for the info guys, I'll remember this for future use.

            Comment

            Working...