[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

BeeJ

3/5/2012 12:20:00 AM

What would you expect from the following code.
No peeking now. Thought experiment only.

Select Case True
Case 0
Debug.Print "0"

Case 1
Debug.Print "1"

Case True
Debug.Print "True"

Case Else ' I was trained to put this here no matter what
' Supposed to make me think that I considered this case.
End Select

Now I don't want a debate about using True in the Select Case because I
do use that and is very convenient.


18 Answers

ralph

3/5/2012 1:21:00 AM

0

On Sun, 04 Mar 2012 16:19:44 -0800, BeeJ <nospam@spamnot.com> wrote:

>What would you expect from the following code.
>No peeking now. Thought experiment only.
>
>Select Case True
>Case 0
> Debug.Print "0"
>
>Case 1
> Debug.Print "1"
>
>Case True
> Debug.Print "True"
>
>Case Else ' I was trained to put this here no matter what
> ' Supposed to make me think that I considered this case.
>End Select
>
>Now I don't want a debate about using True in the Select Case because I
>do use that and is very convenient.
>

LOL!

Let's see if I have this straight?

1) Often reports bizzare results.
2) Likes to use "Highly Recommended To Avoid"* constructs.
3) Is surprised to discover the output of a "Highly Recommended To
Avoid" construct is not what he expected.

Am I the only one here that sees a pattern?

-ralph
(* except in very specific situations)

Thorsten Albers

3/5/2012 10:12:00 AM

0

BeeJ <nospam@spamnot.com> schrieb im Beitrag
<jj10r0$mcn$1@speranza.aioe.org>...
> What would you expect from the following code.
> No peeking now. Thought experiment only.
>
> Select Case True
> Case 0
> Debug.Print "0"
>
> Case 1
> Debug.Print "1"
>
> Case True
> Debug.Print "True"
>
> Case Else ' I was trained to put this here no matter what
> ' Supposed to make me think that I considered this case.
> End Select
>
> Now I don't want a debate about using True in the Select Case because I
> do use that and is very convenient.

What I would expect? To hear that the coder of these lines has been brought
to a sanatorium where he may think all the day about the logic in the code.

--
Thorsten Albers

gudea at gmx.de

Dee Earley

3/5/2012 1:13:00 PM

0

On 05/03/2012 00:19, BeeJ wrote:
> What would you expect from the following code.
> No peeking now. Thought experiment only.

First thought is that it would have printed "True". On testing, it
printed "1", most likely because the input is a boolean, it's coercing
the case expressions to a boolean where non 0 is interpreted as True.

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Larry Serflaten

3/5/2012 1:18:00 PM

0

BeeJ wrote:
> What would you expect from the following code.


I'd expect some Evil Type Conversion. Thats what you get
unless you explicitly type your values.

Try: Select Case CInt(True)

Then you'll get ETC of the Case statements. As you see they
get you coming and going. Are you surprised it's not doing
what you'd expect???

Henning

3/5/2012 2:00:00 PM

0


"BeeJ" <nospam@spamnot.com> skrev i meddelandet
news:jj10r0$mcn$1@speranza.aioe.org...
> What would you expect from the following code.
> No peeking now. Thought experiment only.
>
> Select Case True
> Case 0
> Debug.Print "0"
>
> Case 1
> Debug.Print "1"
>
> Case True
> Debug.Print "True"
>
> Case Else ' I was trained to put this here no matter what
> ' Supposed to make me think that I considered this case.
> End Select
>
> Now I don't want a debate about using True in the Select Case because I do
> use that and is very convenient.
>
>
Just change it to:
Select Case True
Case 0
Debug.Print "0"

Case True
Debug.Print "True"

Case 1
Debug.Print "1"

Case Else ' I was trained to put this here no matter what
' Supposed to make me think that I considered this case.
End Select

And see what you get. ;)
So much for Select Case with multiple True Cases.

/Henning


Jim Mack

3/5/2012 3:19:00 PM

0

> What would you expect from the following code.
> No peeking now. Thought experiment only.
>
> Select Case True
> Case 0
> Debug.Print "0"
>
> Case 1
> Debug.Print "1"
>
> Case True
> Debug.Print "True"
>
> Case Else ' I was trained to put this here no matter what
> ' Supposed to make me think that I considered this case.
> End Select

As other have pointed out, that's not the way you'd use the construct,
even if you allow that it's a reasonable one. In order to be useful and
coherent, the individual cases should evaluate to or be explicitly
coerced to Boolean (CBool), or the test variable coereced to Integer /
Long. As written it depends on implicit conversions, which the
experienced avoid wherever possible.

In any event, it's plain that since a rearrangement of the terms will
produce a different result, it's not a good choice for casual use. It
may be useful, but only if you completely understand its limitations.

--
Jim


Larry Serflaten

3/5/2012 4:11:00 PM

0

BeeJ wrote:
> Thought experiment only.

OK, here's one for you, what gets printed here?

Select Case "True"
Case -1
Debug.Print -1
Case 0
Debug.Print 0
Case 1
Debug.Print 1
Case True
Debug.Print True
Case Else
Debug.Print "Else"
End Select


???

ralph

3/5/2012 4:55:00 PM

0

On Mon, 05 Mar 2012 10:18:44 -0500, Jim Mack <no-uce-ube@mdxi.com>
wrote:

>> What would you expect from the following code.
>> No peeking now. Thought experiment only.
>>
>> Select Case True
>> Case 0
>> Debug.Print "0"
>>
>> Case 1
>> Debug.Print "1"
>>
>> Case True
>> Debug.Print "True"
>>
>> Case Else ' I was trained to put this here no matter what
>> ' Supposed to make me think that I considered this case.
>> End Select
>
>As other have pointed out, that's not the way you'd use the construct,
>even if you allow that it's a reasonable one. In order to be useful and
>coherent, the individual cases should evaluate to or be explicitly
>coerced to Boolean (CBool), or the test variable coereced to Integer /
>Long. As written it depends on implicit conversions, which the
>experienced avoid wherever possible.
>
>In any event, it's plain that since a rearrangement of the terms will
>produce a different result, it's not a good choice for casual use. It
>may be useful, but only if you completely understand its limitations.

As no one has pointed it out yet, I'll add that "VB's True = -1".

Also VB has no 'true' logical operators, everything is actually
"bit-wise". However, in its control and conditional statements ETC
tends to treat all "non-zero" results as 'true'. Which leads to the
common seemingly contradictory ...

value = 1
If value Then
Debug.Print "Value is True" ' value is certainly true enough
End If
If value = True Then
Debug.Print "Value is True"
Else
Debug.Print "Value is False" ' 1 doesn't appear to equal -1
End If

The latter test can even be more interesting in situations where
'value' is a Variant. VB will quite helpfully change the Type from
CInt/CLng to CBoolean, or vice versa. ie, either result is possible.

Other places where trouble may be brewing is ...

Do While value <> True

Never mix scalar values with Boolean values. Your assumptions might be
correct 99% of the time, but that 1% will kill you - usually around
3am in the morning. <bg>

-ralph

Karl E. Peterson

3/6/2012 12:02:00 AM

0

Deanna Earley formulated on Monday :
> most likely because the input is a boolean, it's coercing the case
> expressions to a boolean where non 0 is interpreted as True.

Yep, could've been "Case 3" and it still would've printed 1.

--
..NET: It's About Trust!
http://vfre...


Karl E. Peterson

3/6/2012 12:09:00 AM

0

Larry Serflaten used his keyboard to write :
> BeeJ wrote:
>> Thought experiment only.
>
> OK, here's one for you, what gets printed here?
>
> Select Case "True"
> Case -1
> Debug.Print -1
> Case 0
> Debug.Print 0
> Case 1
> Debug.Print 1
> Case True
> Debug.Print True
> Case Else
> Debug.Print "Else"
> End Select
>
>
> ???

LOL!!!

--
..NET: It's About Trust!
http://vfre...