Click to See Complete Forum and Search --> : Interpret code as it's encountered
Steve Long
03-13-2000, 01:03 PM
Currently VB interprets an entire line of code at once.
Example:
Dim vTmp As Variant
Dim rs As DAO.Recordset
Set rs = OpenRecordset("TableName")
With rs
vTmp = !EmpID
.Edit
!EmpID = IIF(InNull(vTmp), NULL, CLng(vTmp))
.Update
End With
The line with the IIF statement blows up if the vTmp cannot be converted
to a Long (in other words it's a NULL or ""). However, this line of code
should never execute if vTmp is = NULL, yet VB reports a Type Mismatch error
none-the-less.
It would be nice if VB never iterpreted this statement if vTmp was = NULL.
The IIF statement is the only VB construct that is remotely equivalent to
the C++ trinary operator:
test ? expression1 : expression2
However, it works in C++.
How can I submit this request to the VB Team. Does anybody have that URL?
Steve
Alan Gillott
03-13-2000, 01:47 PM
mswish or vbwish@microsoft.com.
This has been the subject of heated debate already on this newsgroup: look
for references to "shortcircuiting"
Alan
Steve Long <steve.long@co.clark.wa.us> wrote in message
news:38cd1f4d$1@news.devx.com...
>
> Currently VB interprets an entire line of code at once.
> Example:
> Dim vTmp As Variant
> Dim rs As DAO.Recordset
>
> Set rs = OpenRecordset("TableName")
>
> With rs
> vTmp = !EmpID
> .Edit
> !EmpID = IIF(InNull(vTmp), NULL, CLng(vTmp))
> .Update
> End With
>
> The line with the IIF statement blows up if the vTmp cannot be converted
> to a Long (in other words it's a NULL or ""). However, this line of code
> should never execute if vTmp is = NULL, yet VB reports a Type Mismatch
error
> none-the-less.
>
> It would be nice if VB never iterpreted this statement if vTmp was = NULL.
> The IIF statement is the only VB construct that is remotely equivalent to
> the C++ trinary operator:
> test ? expression1 : expression2
> However, it works in C++.
>
> How can I submit this request to the VB Team. Does anybody have that URL?
>
> Steve
>
Craig Clearman
03-13-2000, 03:40 PM
Steve,
>The IIF statement is the only VB construct that is remotely equivalent to
>the C++ trinary operator:
>test ? expression1 : expression2
>However, it works in C++.
The VB team will *not* make the change that you are expecting. BASIC
has never had short-circuiting inside its basic functions.
The best you can hope for is a new operator (not IIF) that will
support short-circuiting.
If MS made the change to IIF, how much code do you think would begin
to fail? Don't know? Neither does MS.
Ciao, Craig
Alan Gillott
03-13-2000, 04:36 PM
Except that VB3 used to work that way. I had to change a lot of If
statements when I moved to VB4. It probably wasn't real short circuiting but
allowed me to check the presence of data before referencing it in the same
If statement.
Craig Clearman <chclear@nospam.please> wrote in message
news:2tgqcscv7asqm4fbc2iph1nqn37o6oasth@4ax.com...
> Steve,
>
> >The IIF statement is the only VB construct that is remotely equivalent to
> >the C++ trinary operator:
> >test ? expression1 : expression2
> >However, it works in C++.
>
> The VB team will *not* make the change that you are expecting. BASIC
> has never had short-circuiting inside its basic functions.
>
> The best you can hope for is a new operator (not IIF) that will
> support short-circuiting.
>
> If MS made the change to IIF, how much code do you think would begin
> to fail? Don't know? Neither does MS.
>
> Ciao, Craig
>
Michael \(michka\) Kaplan
03-13-2000, 09:09 PM
This is not entirely true. If you run IIF() through the Jet expression
service (which gets its IIF and other operators from the ES object created
from vbajet32), then it DOES short circuit properly.
I have done work to create these objects in some of my Jet utilities in the
past, but always just to support Jet, never to provide an ES object for
regualr VB usage. But it is theoretically possible now, with binaries
provided by the VB/VBA folks.
--
?MichKa
(insensitive fruitarian)
random junk of dubious value, a multilingual website, the
54-language TSI Form/Report to Data Access Page Wizard,
and lots of replication "stuff" at the (no scripts required!)
http://www.trigeminal.com/
?
"Craig Clearman" <chclear@nospam.please> wrote in message
news:2tgqcscv7asqm4fbc2iph1nqn37o6oasth@4ax.com...
> Steve,
>
> >The IIF statement is the only VB construct that is remotely equivalent to
> >the C++ trinary operator:
> >test ? expression1 : expression2
> >However, it works in C++.
>
> The VB team will *not* make the change that you are expecting. BASIC
> has never had short-circuiting inside its basic functions.
>
> The best you can hope for is a new operator (not IIF) that will
> support short-circuiting.
>
> If MS made the change to IIF, how much code do you think would begin
> to fail? Don't know? Neither does MS.
>
> Ciao, Craig
>
Phil Weber
03-14-2000, 11:39 AM
Steve: Until Microsoft gets around to adding short-circuiting to VB, you can
accomplish the same thing using nested If...Then statements:
If ConditionA Then
If ConditionB Then
' do something
End If
End If
ConditionB is only evaluated if ConditionA is True. The IIF function, of
course, can (and should, in most cases) be replaced by If...Then...Else:
If IsNull(vTmp) Then
!EmpID = Null
Else
!EmpID = CLng(vTmp)
End If
In fact, couldn't the above simply be written !EmpID = vTmp? And since vTmp
contains the value of !EmpID, the code seems to be a big NOP.
---
Phil Weber
Steve Long
03-14-2000, 03:12 PM
Phil, I agree that you could either use a nested If of an If Else construct
to accomplish the same thing. All I was saying is that it would be nice if
the IIF function could actually do this in one line of code like the trinary
operator in C++.
The only reason the whole thing looks like a big NOP is because of the example
I used. In reality I would be getting the value of vTmp from some place else
and putting it into a recordset. I would hopefully never do a big NOP like
that.
Besides, isn't this the wish list place for VB7? I kinda thought that's what
I was posting to.
It's just that it is an awful lot of code (the If Else construct) when you
are dealing with many fields in a recordset.
With this in mind, the IIF statement's functionality is drastically reduced
without actually interpretting each statement as it's encountered.
So, where can I put the wish out there for MS to hear?
Steve
"Phil Weber" <pweber@teleport.com> wrote:
>Steve: Until Microsoft gets around to adding short-circuiting to VB, you
can
>accomplish the same thing using nested If...Then statements:
>
> If ConditionA Then
> If ConditionB Then
> ' do something
> End If
> End If
>
>ConditionB is only evaluated if ConditionA is True. The IIF function, of
>course, can (and should, in most cases) be replaced by If...Then...Else:
>
> If IsNull(vTmp) Then
> !EmpID = Null
> Else
> !EmpID = CLng(vTmp)
> End If
>
>In fact, couldn't the above simply be written !EmpID = vTmp? And since vTmp
>contains the value of !EmpID, the code seems to be a big NOP.
>---
>Phil Weber
>
>
Ronald Dolman
03-18-2000, 11:53 AM
> In fact, couldn't the above simply be written !EmpID = vTmp? And since
vTmp
> contains the value of !EmpID, the code seems to be a big NOP.
Teach them, Phill ;-)
Ronald
devx.com
Copyright WebMediaBrands Inc. All Rights Reserved