2012-04-01 9 views
4

ケースステートメントで2つの条件をテストする方法を理解しようとしています。VB 2010でSelect Caseを使用した複数の条件

Select Case txtWeight.Text 
    Case Is <= 2 
     decShippingCost = (decShipping2 + (decShipping2 * 0.26)) 
    Case Is > 2 and <= 4 
     decShippingCost = (decShipping4 + (decShipping4 * 0.026)) 

私はうまく動作しません。何が間違っていますか?

答えて

7
Select Case txtWeight.Text 
    Case Is <= 2 
     decShippingCost = (decShipping2 + (decShipping2 * 0.26)) 
    Case 3 to 4 
     decShippingCost = (decShipping4 + (decShipping4 * 0.026)) 
End Select 

それとも、このあなたは> 2として2.5をキャッチしたい場合。

Select Case txtWeight.Text 
    Case Is <= 2 
     decShippingCost = (decShipping2 + (decShipping2 * 0.26)) 
    Case 2.01 to 4 
     decShippingCost = (decShipping4 + (decShipping4 * 0.026)) 
End Select 
+0

ありがとう!その常に愚かで小さな何か:)あなたの助けを感謝します! –

+1

'Case Is> 2、Is <= 4' – Valen

5

2番目のケースでは、2つ以上の重みを確認する必要はありません。それが2より大きくない場合、最初のケースに入ります。したがって、これを簡単にすることができます:

Select Case txtWeight.Text 
    Case Is <= 2 
     decShippingCost = (decShipping2 + (decShipping2 * 0.26)) 
    Case Is <= 4 
     decShippingCost = (decShipping4 + (decShipping4 * 0.026)) 
End Select 
+1

それは本当ですが、誰かが最初のケースISをリファクタリングして、それが2番目のケースに影響することを認識していなければ、危険な方法です。しかし、それでも、私の+1はそうする可能性についての情報のためです。 – Stefan

関連する問題