Lazy evaluation?

G

Guest

Hi again,

I've been coding in vb .Net on my most recent project and during a debug
session I noticed that VB .Net hasn't got lazy evaluation... WHY?! I love
lazy evaluation and actually count on it.

In my current application there is data that may or may not exist depending
on a certain value. It's a set of rules that may or may not apply.

At a certain time in the code, there is the following (alike anyhow) statment.
If ((objectA.HasRule4 AND objectB.FailsRule4(params))
OR (objectA.HasRule5 AND objectB.FailsRule5(otherparams))
OR (not objectA.Property.Value.equals(objectB.Property.Value)) then
collectionOfObjectB.Remove(objectB)
else
objectB.Calculatespecialfield()
end if

When I write this code I expect the run to check on objectA.Hasrule4 if that
is false it should move on to the next or-statement.

Is there any possibility to tell VB .Net (or rather VS 2003) to apply lazy
evaluation for conditional statements?

As usual many thanks in advance, and enjoy your holidays :)
 
H

Herfried K. Wagner [MVP]

SL33PY said:
I've been coding in vb .Net on my most recent project and during a debug
session I noticed that VB .Net hasn't got lazy evaluation... WHY?! I love
lazy evaluation and actually count on it.

You may want to use the 'AndAlso' and 'OrElse' operators instead of 'And'
and 'Or'.
 
J

Jay B. Harlow [MVP - Outlook]

SL33PY,
As Herfried suggests, use AndAlso & OrElse; As And & Or are binary logical
operators, while AndAlso & OrElse are Short-Circuiting Logical operators.

http://msdn2.microsoft.com/en-us/library/wz3k228a.aspx

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vaidxlogical.asp

NOTE: AndAlso & OrElse first appeared in .NET 1.0 (VS 2002).

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi again,
|
| I've been coding in vb .Net on my most recent project and during a debug
| session I noticed that VB .Net hasn't got lazy evaluation... WHY?! I love
| lazy evaluation and actually count on it.
|
| In my current application there is data that may or may not exist
depending
| on a certain value. It's a set of rules that may or may not apply.
|
| At a certain time in the code, there is the following (alike anyhow)
statment.
| If ((objectA.HasRule4 AND objectB.FailsRule4(params))
| OR (objectA.HasRule5 AND objectB.FailsRule5(otherparams))
| OR (not objectA.Property.Value.equals(objectB.Property.Value)) then
| collectionOfObjectB.Remove(objectB)
| else
| objectB.Calculatespecialfield()
| end if
|
| When I write this code I expect the run to check on objectA.Hasrule4 if
that
| is false it should move on to the next or-statement.
|
| Is there any possibility to tell VB .Net (or rather VS 2003) to apply lazy
| evaluation for conditional statements?
|
| As usual many thanks in advance, and enjoy your holidays :)
 
C

Cor Ligthert [MVP]

Sl33py,

Is there any possibility to tell VB .Net (or rather VS 2003) to apply lazy
evaluation for conditional statements?
You saw that it is not true, the told reason was that the Or and And should
keep their classic behaviour for backwards compatibility. Something found by
probably most real VB.Net developers not the wise decision afterwards,
especially not if you see how much the VB6->VBNet converters are changing.

However it was done. I hope that backward compatible devil will soon be
forgotten and code will be made in a way that it is as well for not classic
VB users (who know the history) optimal understandable way.

Just my thought,

Cor
 
L

Larry Lard

SL33PY said:
Hi again,

I've been coding in vb .Net on my most recent project and during a debug
session I noticed that VB .Net hasn't got lazy evaluation... WHY?! I love
lazy evaluation and actually count on it.

This isn't really what usually gets the name 'lazy evaluation', btw;
this is usually called 'short-circuited conditionals'. Others have
given you what you need.
 

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