Results 1 to 7 of 7

Thread: .NET cluture shock: #1 Short curcuit evaluation

  1. #1

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    .NET cluture shock: #1 Short curcuit evaluation

    In visual basic 6 (and all previous versions) every component of a logical expression was executed.
    For example if you have:

    VB Code:
    1. If chk1.Value = vbChecked Or chk2.Value = vbChecked Then

    Then even though the second part of the expression can have no effect if the first part is true it is always tested anyway (which is an unneccessary overhead).

    VB.Net introduces two new keywords, OrElse and AndAlso. These perform a lazy evaluation i.e. if the left hand side gives you sufficient information to calculate the logical operator then the right hand side is not evaluated. The keywords or and and still operate as they did but now we have the choice of whether or not to optimise our logical operations.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Thats bizzare, I thought it did that as standard (like Java), as I am defintly sure Ive seen it happen in my code without specifying the keywords myself.

    Didnt know the keywords though.

    On another point, theres a nice function called DirectCast which I didnt know about (use instead of Ctype).

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Grimfort
    Thats bizzare, I thought it did that as standard (like Java), as I am defintly sure Ive seen it happen in my code without specifying the keywords myself.

    Didnt know the keywords though.

    On another point, theres a nice function called DirectCast which I didnt know about (use instead of Ctype).
    I think they did it so it would work by default in the beta versions... the had some other keywords for the current AND and OR I believe...
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Nah, it was a couple of weeks ago, in full VS.Net 2002.
    I noticed it cos I program in other languages and was suprised that it worked.

    I tested it earlier on, and it didnt work I love programming.

  5. #5
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Just curious

    Now that we have "short-circuiting" operators (AndAlso & OrElse)
    Is there ever a time when we'd want to use just plain And or Or?

    I like the sound of "OrElse", I'm going to use that as often as possible in my code!

    Has anyone seen the new "Open-circuiting" operator called False?
    It works like this:
    Code:
    If False Then
        ' All code here is skipped to save time
        ' This can make your code hyper fast!
    End If
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  6. #6
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    You typically use long winded evaluation when you are using functions that do work as well as returning a value e.g.:

    VB Code:
    1. If DeleteUser("djones") AND PurgeLogs("djones") Then
    2.   Trace.WriteLine("User cleared out", Me.GetType.ToString)
    3. End If

    If there was an AndAlso there then the logs wouldn't get purged.

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    umm correct me if I'm wrong, but I think C++ syntax language doesnt have the VB type AND and OR, they just have vb's AndAlso and OrElse....
    since it's like this I would say it's better to just stick with AndAlso and OrElse, just to get used to C++ style programming which seems a little better. Dont use AND or OR
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width