By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

I learned something today (.NET and VB6)

P: n/a
As they say in South Park... "Well, I learned something today."

This is valid in both VB6 and 9 versions:

Select Case TrySelect
Case 1, 3
Debug.Print "1"
Case 2
Debug.Print "2"
Case 3
Debug.Print "3"
End Select

Of course, the "Case 3" never gets executed due to the "Case 1, 3"
catching the 3. I cannot imagine it is even legal to code but does not
even get a simple warning flag in VB9. Good languages will flag code
that cannot get executed. VB6 and 9 both ignore the situation.

Calling this with 1 then 2 then 3 prints 1 then 2 then 1 again.

I accidentally left a duplicate of a Case "test expression" in a program
I am working on. It was a leftover from a change I made some time back.
It had no code in it so I did not notice it until I was reworking the
routine a bit, yesterday. I was amazed that this is even allowed. It
is not allowed in at least one other language I have worked in, possibly
two others. What's the point?

Live and learn!

Mike

Apr 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"AMercer" <AM*****@discussions.microsoft.comschrieb:
Dim i As Integer = 3
Dim j As Integer = 3
Select Case j
Case i : whatever
Case 3 : whatever
End Select

No diagnostic is possible at compile time. What do you want this
construct
to do - throw an exception at runtime when i=3? There are two case
clauses
that match the select clause, but only one will run. I don't think an
exception is useful, and I don't think you do either. Maybe VB allowing
expressions where C allows only constants explains VB's keep-it-simple
approach.
I'd prefer an optional warning or rule which makes the developer aware that
he is writing useless code. However, even 'If False Then...' is permitted
by the compiler.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 10 '08 #2

P: n/a
If False then ... INDEED!
End if

Well, one can shot oneself in the foot, if desired, I guess. I was just
wishing for a warning, at least, when you write something a little more
complex and then it might not run as you think or not run at all.

Thanks for the laugh!

Remember, Fornapulation is fun!

Mike

On Fri, 11 Apr 2008 01:39:12 +0200, in
microsoft.public.dotnet.languages.vb "Herfried K. Wagner [MVP]"
<hi***************@gmx.atwrote:
>"AMercer" <AM*****@discussions.microsoft.comschrieb:
> Dim i As Integer = 3
Dim j As Integer = 3
Select Case j
Case i : whatever
Case 3 : whatever
End Select

No diagnostic is possible at compile time. What do you want this
construct
to do - throw an exception at runtime when i=3? There are two case
clauses
that match the select clause, but only one will run. I don't think an
exception is useful, and I don't think you do either. Maybe VB allowing
expressions where C allows only constants explains VB's keep-it-simple
approach.

I'd prefer an optional warning or rule which makes the developer aware that
he is writing useless code. However, even 'If False Then...' is permitted
by the compiler.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.