Re: RE: I learned something today (.NET and VB6)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



VERY interesting point. I have never even considered the possibility of
coding what you show here. I had no idea it was even legal. However,
it still has a problem. The second Case never runs as far as I can tell
just desk checking it. It is just the same as my original assertion.
You get non-executable code which is unflagged.

Mike

On Thu, 10 Apr 2008 13:49:01 -0700, in
microsoft.public.dotnet.languages.vb AMercer
<AMercer@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Well, you have a point, I suppose. The existing behavior is documented, to
wit "If testexpression matches an expressionlist clause in more than one Case
clause, only the statements following the first match run."

Why is it this way? Maybe just sloppy work at MS, but a more interesting
explanation (I don't know if it is true or not) comes from the fact that in
"case xxx", xxx can be an variable (rather than just a constant as in C), eg

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'm not asserting that this explanation is the truth - it is just musings
that follow from your original post.


"Just_a_fan@xxxxxxxx" wrote:

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



.



Relevant Pages

  • RE: I learned something today (.NET and VB6)
    ... wit "If testexpression matches an expressionlist clause in more than one Case ... expressions where C allows only constants explains VB's keep-it-simple ... even get a simple warning flag in VB9. ... Good languages will flag code ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Too much code - slicing
    ... then false clause. ... so it preserves the logical flow. ... expressions, but to point out the LOGICAL ORDER. ... in every other language I use that lets me use an if/else as ...
    (comp.lang.python)
  • Re: UNION ALL
    ... I am assuming that you are talking about in the where clause. ... might better be represented by mulitple expressions connected by an OR? ... I believe that multiple CASE statements will ... > parse faster because you are only using one main query as ...
    (microsoft.public.sqlserver.programming)
  • Re: Whats so great about lisp?
    ... There are also many languages with more sophisticated type systems that can represent and statically check lots of much more complicated constraints, e.g. Haskell and theorem provers such a Coq. ... > (define (map f l) ... be no more difficult than it was to implement pattern matching in ML. ...
    (comp.lang.lisp)
  • Re: Excellent
    ... If you were writing SQL, ... would be treated as an abstract data type. ... Start in the FROM clause and build a working table from all of the ... e) Go to the SELECT clause and construct the expressions in the list. ...
    (microsoft.public.sqlserver.programming)