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 21 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
    }

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

    That reminds me of a nice codebase that used to refuse to compile in Release mode due to unbalanced brackets - but it was fine in Debug mode.

    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
      }

      R Offline
      R Offline
      RugbyLeague
      wrote on last edited by
      #3

      That could open up a whole world of trouble

      P 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
        }

        R Offline
        R Offline
        rhuiden
        wrote on last edited by
        #4

        What's wrong with this VB code?

        If test Then
        	do stuff
        

        #If Not Debug Then
        Else
        #End If
        do other stuff
        End If

        F P 2 Replies Last reply
        0
        • R rhuiden

          What's wrong with this VB code?

          If test Then
          	do stuff
          

          #If Not Debug Then
          Else
          #End If
          do other stuff
          End If

          F Offline
          F Offline
          fjdiewornncalwe
          wrote on last edited by
          #5

          It's VB... ( No offense to VB Devs out there. I just personally hate it because I've had to fix way to much COM code written in VB by guys who were "self-taught", but not in the good way )

          1 Reply Last reply
          0
          • R rhuiden

            What's wrong with this VB code?

            If test Then
            	do stuff
            

            #If Not Debug Then
            Else
            #End If
            do other stuff
            End If

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

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

            A D 2 Replies Last reply
            0
            • P PIEBALDconsult

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

              A Offline
              A Offline
              Andrew Rissing
              wrote on last edited by
              #7

              Not that I'm a VB developer, but I believe this would work:

              If test Then
              do stuff
              #If Not Debug Then
              Else
              #Else
              End If
              If True Then
              #End If
              do other stuff
              End If

              It may not win any beauty pageants, but it would do the same.

              P 1 Reply Last reply
              0
              • R RugbyLeague

                That could open up a whole world of trouble

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

                Yes, and I hope to find a better fix. At least it's clearer in C# than VB.

                1 Reply Last reply
                0
                • A Andrew Rissing

                  Not that I'm a VB developer, but I believe this would work:

                  If test Then
                  do stuff
                  #If Not Debug Then
                  Else
                  #Else
                  End If
                  If True Then
                  #End If
                  do other stuff
                  End If

                  It may not win any beauty pageants, but it would do the same.

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

                  Yes, but can see how much more confusing it is. As well as adding a needless if.

                  A 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Yes, but can see how much more confusing it is. As well as adding a needless if.

                    A Offline
                    A Offline
                    Andrew Rissing
                    wrote on last edited by
                    #10

                    While I do agree, this is a niche case though. This is hardly typical behavior needed by most programs. If I saw your code in C# (or even the VB version), I'd probably do a double take either way. :]

                    P 1 Reply Last reply
                    0
                    • A Andrew Rissing

                      While I do agree, this is a niche case though. This is hardly typical behavior needed by most programs. If I saw your code in C# (or even the VB version), I'd probably do a double take either way. :]

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

                      Only double? :~

                      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
                        }

                        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