Explanation Please

G

Greg

Will one of the Masters please explain why Sub Test() below will work
buy Test1() won't?

Sub Test()
Dim oAge As String
Retry:
oAge = InputBox("How old are you in years?", "Age")
If Not IsNumeric(oAge) Then GoTo Retry
MsgBox "You are " & oAge & " years old."
End Sub

Sub Test1()
Dim oAge As String
Retry:
If Not IsNumeric(oAge = InputBox("How old are you in years?", "Age"))
Then GoTo Retry
MsgBox "You are " & oAge & " years old."
End Sub

I realize that in practice it might be better to declare oAge as Long
and perhaps use an error handler. I would just like to understand why
the construction of Test1() won't work.
 
J

Jonathan West

Hi Greg,

The = sign has two different meanings, depending on context.

For a statement that starts with a variable or property, = stands for
assignment. The value to the right of the = sign is assigned to the variable
or property to the left.

For all other statements, the = sign means a test for equality in an
expression, and the expression returns True or False depending on whether
the values to the left & right of the = sign are the same.

In the Test macro, the = is an assignment operator, assigning the return
value of the InputBox function to the oAge variable.

In the Test1 macro, things are a bit more complex, but I'll unpack it. The
key line is this

If Not IsNumeric(oAge = InputBox("How old are you in years?", "Age")) Then
GoTo Retry

(all that should be on one line)

Working from the outside in, we have this

If the expression between "If" and "Then" is True, then you Goto Retry

That expression is Not IsNumeric(oAge = InputBox("How old are you in
years?", "Age"))

IsNumeric is True if oAge = InputBox("How old are you in years?", "Age")) is
numeric

But oAge = InputBox("How old are you in years?", "Age")) is an expression
that includes an equality operator, in other words, it is True or False
depending on whether the return value of the InputBox function is equal to
the current value of oAge. IsNumeric(True) and IsNumeric(False) both return
True, which means that the overall expression is always False, and you never
follow the Goto.

But remember that the = sign in this context is an equality operator, not an
assignment operator, so oAge will still be a null string, so the message box
on the following line will always say "You are years old"

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi everybody,

and furthermore, in Test1 no assigment to oAge is done at all.
so you compare an empty variable with what the messagebox returns.

' some examples more

Dim oAge As String
Dim aBln As Boolean
aBln = False
MsgBox IsNumeric(aBln) ' boolean is numeric, by the way
MsgBox InputBox("How old are you in years?")
If InputBox("How old are you in years?") = 55 Then MsgBox "true"
MsgBox oAge = InputBox("How old are you in years?")
MsgBox oAge ' nothing

Helmut Weber
 
J

Jay Freedman

Greg said:
Jonathan,

Copy all. Thanks for the explanation.

Just to be perverse <g>, although the kind of syntax you tried to use in
Test1() won't work in VBA or VB, it can be used in C++.

In those languages, there are two distinct operators, = for assignment and
== for logical equality. If you nest an assignment inside an equality test,
the result of the assignment serves as the value of the expression. For
example, I can write

if ( (Result = Somefunction(x)) == 100) { do something else; }

First Somefunction(x) will be evaluated, and its value will be assigned to
the variable Result. That will also be the value of the assignment
expression. Then the value of the assignment expression will be compared to
100, and the statement in braces will be executed if the comparison yields
the logical value True.

Although this kind of shorthand can be useful, it also creates an
opportunity for bugs when you use = where you should use ==. To try to avoid
that, most C++ compilers will give a warning when you use = inside an if
expression.

Now try to forget what I just told you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top