views:

50

answers:

3

So I have case statements grouped together here, but I have a few case statements that need extra decision based on a second variable after the original case is confirmed.

This is what I have now:

Case "PRIVATE_PARTY"
    If Condition = KBB_CONDITION_EXCELLENT Then
        Vehicle.MarketValue = Response.PrivatePartyExcellent
    ElseIf Condition = KBB_CONDITION_GOOD Then
        Vehicle.MarketValue = Response.PrivatePartyGood
    Else
        Vehicle.MarketValue = Response.PrivatePartyFair
    End If

Is it possible to add an "and" statement to some of the cases like this and have the code work in the same fashion?

Case "TRADE_IN" And Condition = KBB_CONDITION_EXCELLENT
    Vehicle.MarketValue = Response.TradeInExcellent

And then just have 3 case statements instead of one but the code wouldn't look ugly. By the way Condition is declared instead the same select.

  1. Would this work?
  2. Is there any reason why I shouldn't use this if it does work?
+1  A: 

I think you can have a condition on only one variable. You could add a select case inside the select case, but I doubt you can do better.

http://msdn.microsoft.com/en-gb/library/cy37t14y%28v=VS.80%29.aspx

GôTô
+1  A: 
  1. Not with that exact syntax, no
  2. See 1. :-)

One technique you can use here is to take advantage of the one of the things that VB.NET allowed that C# doesn't - namely, non-constant Case conditions (I have guessed at the variable Seller):

Select Case True
Case Seller = "TRADE_IN" And Condition = KBB_CONDITION_EXCELLENT
    Vehicle.MarketValue = Response.TradeInExcellent
Case Seller = "PRIVATE_PARTY" Condition = KBB_CONDITION_EXCELLENT
    Vehicle.MarketValue = Response.PrivatePartyExcellent
' etc
End Select

The Cases will be evaluated in order until one is True.


However! If all this Select Case is doing is mapping from a combination of seller and condition to a Response, I would suggest that a better way of doing the whole operation is to (just once) set up a nested Dictionary containing the mapping data, then look up the values in it whenever you want a Response. I can expand on this if you're interested...

AakashM
I don't see how your "Select Case True" is better than an "If"
GôTô
@GoTo: Well, if you really want to use a Select Case statement, this is the way to go.
Alex Essilfie
+3  A: 

Short Answer

The code might compile, but it will be wrong. Don't try to use Select Case like this! I explain why below.


Long Answer

Select Case in VB.NET is basically a glorified If/ElseIf/etc. (it is not semantically the same as a switch). So attempting to write your code this way would actually offer no real benefit over writing the equivalent code using If, ElseIf, etc.

That said, the below does compile with Option Strict Off and Option Infer On using VB 9.

Note: I am not saying this will "work" just because it compiles. The point I am in the process of making here is that just because code compiles, that doesn't mean it does what you expect. Again: this compiles, but it will not work. Don't use it.*

Select Case s
    Case "Private Party" AndAlso i = 1
        Console.WriteLine(1)
    Case "Trade In" AndAlso i = 2
        Console.WriteLine(2)
End Select

The question is: what does it compile to?

Cracking open the code in Reflector, here's what you see:

Dim VB$t_string$L0 As String = s
If (VB$t_string$L0 = Conversions.ToString((Conversions.ToBoolean("Private Party") AndAlso (i = 1)))) Then
    Console.WriteLine(1)
ElseIf (VB$t_string$L0 = Conversions.ToString((Conversions.ToBoolean("Trade In") AndAlso (i = 2)))) Then
    Console.WriteLine(2)
End If

So the VB compiler interprets "Private Party" AndAlso i = 1 as a Boolean, which it will then convert to a String (either "True" or "False") in order to compare it to s.

In other words, this isn't going to work the way you want it to. I'd honestly just go for the If/ElseIf block; it would be easier to read (in my opinion).

*You probably already understood the point I was making; I just wanted to make it 100% clear ;)

Dan Tao
Turning Option Strict Off for that is not great
GôTô
@GôTô: Haha, nobody called it "great." I'm strongly recommending *against* it. But, for better or worse, a lot of VB.NET developers (in my experience) write code with Option Strict Off. Such developers could write the snippet I posted, see that it compiles, and pat themselves on the back. I wanted to make clear what the behavior was.
Dan Tao
You recommend against it but still give the code, and it got accepted...
GôTô
@GôTô: I get the feeling you have not actually read my answer.
Dan Tao
I've read that you give the advice to stick with the If/ElseIf, I just find weird to give code and say "don't use it"
GôTô
@GôTô: The OP asked 1) if the code would "work"; and 2) if there were any reason not to use it. I didn't want to simply say "No" to the first question, because that could mislead the OP into thinking that if he tried it and it compiled, I would be proven wrong and my answer would be thus invalidated. The fact is that it *could* compile, but it is still *wrong*. I feel my answer is quite clear on that point. Nowhere do I in any way suggest that the `Option Strict Off` code is a good idea at all. But you've convinced me; I'll add a note in bold just so there's no mistaking my point.
Dan Tao
@GôTô: I get the feeling you're a C-based programmer [who writes off VB programmers as a little above MS Excel users] or you're a novice to programming. Neither of them is good enough.
Alex Essilfie
@Dan Tao: Sorry for being pernickety, I didn't want to piss you off (which I guess I did considering your edit).
GôTô
@Alex Essilfie: I'm not a C-based programmer, I used VB6 and VB.Net way more than C or C++ or even C#, so I guess I would be in the second category... Actually I prefer VB Select Case to C switch as it is more permissive (you can compare to non const values). I think Strict option should be turned off when there is no other choice, not to save two lines of code. And thanks for saying I am not good enough...
GôTô
@GôTô: Sorry, man, you didn't piss *me* off; from your comments I was under the impression that *I* had pissed *you* off, hence my edits to try and explain that I wasn't recommending the first piece of code just by saying it compiled.
Dan Tao
@Dan Tao: No problem
GôTô