Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Here's something else VB can't do

Here's something else VB can't do

Scheduled Pinned Locked Moved Clever Code
debugging
21 Posts 10 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PIEBALDconsult

    (At least not as easily as far as I can tell.) Today I was working on some code that involves an if/else, but decided that when debugging, I wanted the else-block to execute regardless of the test. I came up with this technique:

    if ( test )
    {
        do stuff
    }
    

    # if !DEBUG
    else
    # endif
    {
    do other stuff
    }

    T Offline
    T Offline
    Turro
    wrote on last edited by
    #12

    Sorry to reply, but... VB (.NET!) can do! Of course the syntax is ... exactly the same:

    If Test = 1 Then
      ' Do Ugly Stuff Here Just Because It's Not C#
    

    #If DEBUG Then
    Else
    #End If
    ' Do Other Nasty Stuff Here Just Because It's Not C#

    End If
    

    But don't trust me, dirt your hands writing a little VB code... Moreover, this is not the only possible techniques: you could also use the System.Diagnostics.Debugger class by inspecting the IsAttached property. Of course this class cannot be use to include/exclude only the else keyword like the example you provided.

    ============= Marco Turrini

    P 1 Reply Last reply
    0
    • T Turro

      Sorry to reply, but... VB (.NET!) can do! Of course the syntax is ... exactly the same:

      If Test = 1 Then
        ' Do Ugly Stuff Here Just Because It's Not C#
      

      #If DEBUG Then
      Else
      #End If
      ' Do Other Nasty Stuff Here Just Because It's Not C#

      End If
      

      But don't trust me, dirt your hands writing a little VB code... Moreover, this is not the only possible techniques: you could also use the System.Diagnostics.Debugger class by inspecting the IsAttached property. Of course this class cannot be use to include/exclude only the else keyword like the example you provided.

      ============= Marco Turrini

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #13

      As mentioned when someone else suggested that... that is not the same.

      T 1 Reply Last reply
      0
      • P PIEBALDconsult

        As mentioned when someone else suggested that... that is not the same.

        T Offline
        T Offline
        Turro
        wrote on last edited by
        #14

        True, my bad, I misunderstood that else. But as Andrew Rissing showed you, this is perfectly possible even with such a language like VB(.NET), which was the point of the original post "something else VB can't do": true if you refer to old VB6, false if you refer to VB.NET. Of course no language can compete to the beauty of C(#), but this comes more to personal preference and habit rather than mere possibility to do things.

        ============= Marco Turrini

        P 1 Reply Last reply
        0
        • T Turro

          True, my bad, I misunderstood that else. But as Andrew Rissing showed you, this is perfectly possible even with such a language like VB(.NET), which was the point of the original post "something else VB can't do": true if you refer to old VB6, false if you refer to VB.NET. Of course no language can compete to the beauty of C(#), but this comes more to personal preference and habit rather than mere possibility to do things.

          ============= Marco Turrini

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #15

          Turro wrote:

          can't do

          I qualified that at the top of my post.

          1 Reply Last reply
          0
          • P PIEBALDconsult

            (At least not as easily as far as I can tell.) Today I was working on some code that involves an if/else, but decided that when debugging, I wanted the else-block to execute regardless of the test. I came up with this technique:

            if ( test )
            {
                do stuff
            }
            

            # if !DEBUG
            else
            # endif
            {
            do other stuff
            }

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #16

            I'd use that for troubleshooting, but never commit that.

            Agh! Reality! My Archnemesis![^]
            | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

            1 Reply Last reply
            0
            • P PIEBALDconsult

              (At least not as easily as far as I can tell.) Today I was working on some code that involves an if/else, but decided that when debugging, I wanted the else-block to execute regardless of the test. I came up with this technique:

              if ( test )
              {
                  do stuff
              }
              

              # if !DEBUG
              else
              # endif
              {
              do other stuff
              }

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #17

              Thank god. Now we just need to remove the ability to do that from C#.

              [Forum Guidelines]

              1 Reply Last reply
              0
              • P PIEBALDconsult

                (At least not as easily as far as I can tell.) Today I was working on some code that involves an if/else, but decided that when debugging, I wanted the else-block to execute regardless of the test. I came up with this technique:

                if ( test )
                {
                    do stuff
                }
                

                # if !DEBUG
                else
                # endif
                {
                do other stuff
                }

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #18

                Oh, and just for the heck of it, here is an alternative for VB, which is less optimal but arguably cleaner looking.

                Dim doElse As Boolean = True
                If test Then
                doElse = False
                ' Do Stuff.
                End If
                IfDebugThenTrue(doElse)
                If doElse Then
                ' Do other stuff.
                End If

                You'd need this support method somewhere:

                ' In a land, far far away...
                <Conditional("DEBUG")> _
                Private Sub IfDebugThenTrue(ByRef myBool As Boolean)
                myBool = True
                End Sub

                [Forum Guidelines]

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  That adds the else to the then rather than separate the else from the if entirely.

                  D Offline
                  D Offline
                  djdanlib 0
                  wrote on last edited by
                  #19

                  I was tired when I first read that sentence, and my brain threw a parse exception on the first pass. So I had to read it again and it makes sense the second time. If you presented that sentence out of context to an English grammar teacher who doesn't understand code, their head would explode. Be sure to put it on Youtube :laugh:

                  P 1 Reply Last reply
                  0
                  • D djdanlib 0

                    I was tired when I first read that sentence, and my brain threw a parse exception on the first pass. So I had to read it again and it makes sense the second time. If you presented that sentence out of context to an English grammar teacher who doesn't understand code, their head would explode. Be sure to put it on Youtube :laugh:

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #20

                    Ehhhxcellent... :cool:

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      (At least not as easily as far as I can tell.) Today I was working on some code that involves an if/else, but decided that when debugging, I wanted the else-block to execute regardless of the test. I came up with this technique:

                      if ( test )
                      {
                          do stuff
                      }
                      

                      # if !DEBUG
                      else
                      # endif
                      {
                      do other stuff
                      }

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #21

                      Funny, I just did that in my C++ code the other day. I just had to experiment and you are correct, what is simple in C/C++ will never work in VB. Nice technical point. I am far more used to C and C# programming and starting a VB contract job next week. This is a good heads up for me. Thanks. I tried this is VB in VS2008 as a console app and it seems more or less equivalent to your sample: Module Module1 Sub Main() Dim iNumber As Integer Dim tText As String iNumber = 99 tText = "Is iNumber less than 100?" If (iNumber < 100) Then tText = "iNumber is less than 100" #If DEBUG Then iNumber = iNumber + 2 <<<<<<<<<<< GETS HERE FINE IN DEBUG. BUT, GOES FROM HERE TO End If #End If #If DEBUG Then Else tText = "iNumber is more than 100" <<<<<<<<<<< NEVER RUNS THIS CODE iNumber = iNumber + 7 #End If iNumber = iNumber - 19 <<<<<<<<<<< NEVER GETS HERE, JUST SKIPS IT End If Console.WriteLine(tText) Console.WriteLine(iNumber) End Sub End Module OUTPUT: iNumber is less than 100 101

                      TW Burger

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups