Hey, I'm using Vb6 and I have a problem with my code but I don't know why.
Here it is: -
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.
5 9317
I was looking at random code and it gave me an idea, so I tried: -
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.
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: - 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
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... - 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.
...
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.
That's for the info guys, I'll remember this for future use.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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{
...
|
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...
|
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...
|
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
|
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.
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |