473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. 'Opacity -------------------------
  4. Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  5. Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  6. Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
  7.  
  8. Private Const GWL_EXSTYLE = (-20)
  9. Private Const WS_EX_LAYERED = &H80000
  10. Private Const LWA_ALPHA = &H2&
  11. '--------------------------------------
  12.  
  13. Private Function Opacity(Value As Byte, Frm As Form)
  14. On Error GoTo ErrorHandler
  15.  
  16. Dim MaxVal As Byte, MinVal As Byte
  17.  
  18. MinVal = 20: MaxVal = 255
  19.  
  20. If Value > MaxVal Then Value = MaxVal
  21. If Value < MinVal Then Value = MinVal
  22.  
  23. SetWindowLongA Frm.hwnd, GWL_EXSTYLE, GetWindowLongA(Frm.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
  24. SetLayeredWindowAttributes Frm.hwnd, 0, Value, LWA_ALPHA
  25.  
  26. ErrorHandler:   Exit Function
  27. End Function
  28.  
  29. Private Sub hsbOpacity_Change()
  30. Opacity hsbOpacity.Value, Me
  31. End Sub
  32.  
  33. Private Sub Text1_Change()
  34. n = Text1
  35. For i = 0 To n - 1
  36. NewStr = NewStr + Text2.Text + ";"
  37. Next i
  38. Text3 = NewStr
  39. End Sub
  40.  
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.
Jan 21 '08 #1
5 9317
I was looking at random code and it gave me an idea, so I tried:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_Change()
  2. Dim n As Variant
  3. Dim i As Variant
  4. Dim NewStr As String
  5. n = Text1
  6. For i = 0 To n - 1
  7. NewStr = NewStr + Text2.Text + ";"
  8. Next i
  9. Text3 = NewStr
  10. End Sub
And it has worked.
Jan 21 '08 #2
kadghar
1,295 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. Remove the line 'Option Explicit' (i dont recomend you doing this, since i think using option explicit is a good habit), or
  2. 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
Jan 21 '08 #3
Killer42
8,435 Expert 8TB
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...
Expand|Select|Wrap|Line Numbers
  1. For Counter = 1 To nnnnnnn
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.
Jan 22 '08 #4
kadghar
1,295 Expert 1GB
...
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.
Jan 22 '08 #5
That's for the info guys, I'll remember this for future use.
Jan 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ken Fine | last post by:
I have a Sub which defines a variable. I'm wondering if there's anyway I can get at that variable outside of the context of the Sub -- to pass the variable's contents to another page, for instance....
2
by: MC | last post by:
HI to all, First excuse me but i'm xsl beginner. I'm using XSL to transform an xml file into another. I'm using a Java program for the transformation (TraX API). I would like to recover in...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
5
by: jeet_sen | last post by:
Hi, My external javascript test.js contains a variable definition var a = "Hello,world!!"; I dynamically loaded the script test.js using the following fucntion: function loadScript(url) { var...
2
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B | last post by:
Please see the followed example: class A: def __init__(self): pass class X: def __init__(self): n = 200 if True: j = 200
1
by: Meal | last post by:
Is this safe? foo(C2X("")); C2X is a macro defined as #define C2X(str) XStr(str).unicodeForm() XStr is a kind of string processing class. unicodeForm() returns pointer of its member string.
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: arsha123 | last post by:
How can I change the value of empty session variable with another variable value defined in form. I get value of session value of id from a logon form , that is "". I have defined a variable in form...
3
by: kino | last post by:
Hello, I have a variable in a ".cs.aspx" file and I want to use it in the same page's ".aspx" file, I tried using Reponse.Write with the help of the syntax: <%Response.Write(var)%> But it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.