Here's something else VB can't do
-
(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
}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
-
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
As mentioned when someone else suggested that... that is not the same.
-
As mentioned when someone else suggested that... that is not the same.
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
-
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
Turro wrote:
can't do
I qualified that at the top of my post.
-
(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
}I'd use that for troubleshooting, but never commit that.
Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy -
(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
}Thank god. Now we just need to remove the ability to do that from C#.
-
(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
}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 IfYou'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 -
That adds the else to the then rather than separate the else from the if entirely.
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:
-
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:
Ehhhxcellent... :cool:
-
(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
}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