案例陈述问题 -- vb.net
因此,我在这里将案例陈述分组在一起,但有一些案例陈述需要在确认原始案例后根据第二个变量进行额外决策。
这就是我现在所拥有的:
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
是否可以向像这样的某些情况添加“and”语句并使代码以相同的方式工作?
Case "TRADE_IN" And Condition = KBB_CONDITION_EXCELLENT
Vehicle.MarketValue = Response.TradeInExcellent
然后只需 3 个 case 语句而不是 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.
- Would this work?
- Is there any reason why I shouldn't use this if it does work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短回答
代码可能会编译,但会错误。不要尝试像这样使用
Select Case
!我在下面解释原因。VB.NET 中的长答案
Select Case
基本上是一个美化的If
/ElseIf
/etc。 (它在语义上与switch
不相同)。因此,尝试以这种方式编写代码实际上不会比使用If
、ElseIf
等编写等效代码提供任何真正的好处。也就是说,下面的代码确实使用
进行编译使用 VB 9 的 >Option Strict Off
和Option Infer On
。注意:我不是说这会“起作用”,只是因为它编译。。我在这里提出的观点是,仅仅因为代码可以编译,并不意味着它会达到您的预期。再说一次:这个可以编译,但是不起作用。不要使用它。*
问题是:它编译成什么??
在 Reflector 中破解代码,您会看到以下内容:
因此 VB 编译器将
"Private Party" AndAlso i = 1
解释为Boolean
,然后将其转换为String
(“True”或“False”),以便将其与s
进行比较。换句话说,这不会按照您希望的方式工作。老实说,我只想使用
If
/ElseIf
块;它会更容易阅读(在我看来)。*你可能已经明白我的意思了;我只是想让它 100% 清楚;)
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 glorifiedIf
/ElseIf
/etc. (it is not semantically the same as aswitch
). So attempting to write your code this way would actually offer no real benefit over writing the equivalent code usingIf
,ElseIf
, etc.That said, the below does compile with
Option Strict Off
andOption 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.*
The question is: what does it compile to?
Cracking open the code in Reflector, here's what you see:
So the VB compiler interprets
"Private Party" AndAlso i = 1
as aBoolean
, which it will then convert to aString
(either "True" or "False") in order to compare it tos
.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 ;)
我认为你可以只对一个变量设置条件。
您可以在选择案例中添加一个选择案例,但我怀疑您可以做得更好。
http://msdn.microsoft.com/en -gb/library/cy37t14y%28v=VS.80%29.aspx
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
您可以在此处使用的一项技术是利用 VB.NET 允许而 C# 不允许的功能之一 - 即非常量
Case< /code> 条件(我猜测了变量
Seller
):Case
将按顺序进行评估,直到其中一个为True
。然而!如果所有这些
Select Case
所做的都是从卖家和条件的组合映射到Response
,我建议执行整个操作的更好方法是(只是一次)设置一个包含映射数据的嵌套字典
,然后在需要响应
时查找其中的值。如果您有兴趣,我可以对此进行扩展...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 variableSeller
):The
Case
s will be evaluated in order until one isTrue
.However! If all this
Select Case
is doing is mapping from a combination of seller and condition to aResponse
, I would suggest that a better way of doing the whole operation is to (just once) set up a nestedDictionary
containing the mapping data, then look up the values in it whenever you want aResponse
. I can expand on this if you're interested...