1

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?
3
  • Just curious, didn't it take you longer to type this question than it would have taken to make this trivial change in the code itself where you could see for yourself if it compiled or not? Commented Oct 6, 2010 at 13:57
  • @tnyfst: Whether "it compiled or not" isn't the right question, though -- it should be whether "it worked or not." Commented Oct 6, 2010 at 14:05
  • I also wanted to know from a design standpoint. Commented Oct 6, 2010 at 14:29

3 Answers 3

3

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 ;)

Sign up to request clarification or add additional context in comments.

9 Comments

Turning Option Strict Off for that is not great
@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.
@GôTô: I get the feeling you have not actually read my answer.
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ô: 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.
|
1

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

Comments

1
  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...

2 Comments

I don't see how your "Select Case True" is better than an "If"
@GoTo: Well, if you really want to use a Select Case statement, this is the way to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.