案例陈述问题 -- vb.net

发布于 2024-09-26 07:32:18 字数 712 浏览 1 评论 0原文

因此,我在这里将案例陈述分组在一起,但有一些案例陈述需要在确认原始案例后根据第二个变量进行额外决策。

这就是我现在所拥有的:

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 个,但代码看起来不会难看。顺便说一句,条件被声明为相同的选择。

  1. 这行得通吗?
  2. 如果它有效的话,我有什么理由不应该使用它吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

优雅的叶子 2024-10-03 07:32:18

简短回答

代码可能会编译,但会错误。不要尝试像这样使用Select Case!我在下面解释原因。


VB.NET 中的长答案

Select Case 基本上是一个美化的 If/ElseIf/etc。 (它在语义上与switch相同)。因此,尝试以这种方式编写代码实际上不会比使用 IfElseIf 等编写等效代码提供任何真正的好处。

也就是说,下面的代码确实使用 进行编译使用 VB 9 的 >Option Strict OffOption Infer On

注意:我不是说这会“起作用”,只是因为它编译。。我在这里提出的观点是,仅仅因为代码可以编译,并不意味着它会达到您的预期。再说一次:这个可以编译,但是不起作用。不要使用它。*

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

问题是:它编译成什么?

在 Reflector 中破解代码,您会看到以下内容:

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

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

素衣风尘叹 2024-10-03 07:32:18

我认为你可以只对一个变量设置条件。
您可以在选择案例中添加一个选择案例,但我怀疑您可以做得更好。

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

拍不死你 2024-10-03 07:32:18
  1. 没有那种精确的语法,请
  2. 参阅 1。:-)

您可以在此处使用的一项技术是利用 VB.NET 允许而 C# 不允许的功能之一 - 即非常量 Case< /code> 条件(我猜测了变量 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

Case 将按顺序进行评估,直到其中一个为 True


然而!如果所有这些 Select Case 所做的都是从卖家和条件的组合映射到 Response,我建议执行整个操作的更好方法是(只是一次)设置一个包含映射数据的嵌套字典,然后在需要响应时查找其中的值。如果您有兴趣,我可以对此进行扩展...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文