开发者

Tidying a complicated if-else statement

开发者 https://www.devze.com 2023-02-27 12:16 出处:网络
I\'m trying to find a tidy way to set a string using an if else statement according to a numeric value.

I'm trying to find a tidy way to set a string using an if else statement according to a numeric value.

The only complication is that there is a possibility that this value may contain text. So it needs to handle that situation too.

So this is the basic code:

                        If cint(QtyShop) > 0 Then
                            msg = "Stock found in this shop"
                        ElseIf cint(QtyAllShops) > 0 Then
                            msg = "Stock found in a different shop"
                        Else
                            msg= "No stock found anywhere"
                        End If

The trouble is that if QtyShop has a text value rather than numeric it will th开发者_开发百科row an error. I need it to simply treat this scenario as "false", and continue to the next elseif.

At the moment, the only solution I can think of is this ugly monstrosity:

                        If IsNumeric(QtyShop) Then
                            If cint(QtyShop) > 0 Then
                                msg = "Stock found in this shop"
                            ElseIf IsNumeric(QtyAllShops) Then
                                If cint(QtyAllShops) > 0 Then
                                    msg = "Stock found in a different shop"
                                Else
                                    msg = "No stock found anywhere"
                                End If
                            Else
                                msg = "No stock found anywhere"
                            End If
                        Else
                            If IsNumeric(QtyAllShops) Then
                                If cint(QtyAllShops) > 0 Then
                                    msg = "Stock found in a different shop"
                                Else
                                    msg = "No stock found anywhere"
                                End If
                            Else
                                msg = "No stock found anywhere"
                            End If
                        End If

It makes me feel very sad when I look at it. :- ( Please can someone make it go away.


You should separate the handling of business logic (“what is the quantity?”) and validation.

So first test whether the user input is a valid number. Then put that number into a numeric variable – don’t handle String variables as if they were numbers.

You can make this much easier by enabling Option Strict. This will cause the compiler to report errors in your code because you are treating strings as numbers.

Finally, consider early return: return the result as soon as it’s found. This way you can avoid deeply nested statements:

' Input validation ….

If QtyShop > 0 Then Return "Stock found in this shop"

If QtyAllShops > 0 Then Return "Stock found in a different shop"

Return "No stock found anywhere"
0

精彩评论

暂无评论...
验证码 换一张
取 消