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
}That could open up a whole world of trouble
-
(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
} -
What's wrong with this VB code?
If test Then do stuff
#If Not Debug Then
Else
#End If
do other stuff
End IfIt'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 )
-
What's wrong with this VB code?
If test Then do stuff
#If Not Debug Then
Else
#End If
do other stuff
End IfThat adds the else to the then rather than separate the else from the if entirely.
-
That adds the else to the then rather than separate the else from the if entirely.
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 IfIt may not win any beauty pageants, but it would do the same.
-
That could open up a whole world of trouble
Yes, and I hope to find a better fix. At least it's clearer in C# than VB.
-
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 IfIt may not win any beauty pageants, but it would do the same.
Yes, but can see how much more confusing it is. As well as adding a needless if.
-
Yes, but can see how much more confusing it is. As well as adding a needless if.
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. :]
-
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. :]
Only double? :~
-
(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