A friend asked me for help today about a segment of code that was giving him a headache. After looking at the code I immediately told him the problem. Can you spot the problem too without compiling the VB code? Why is it happening?
Private Sub Calculate()
Dim value1 As Integer = 0
‘Imagine that the value of value1 is has been manipulated on this section
‘///–> Insert imaginary code here <—
Dim value2 As Integer = CInt(IIf(value1 <= 0, 100, CalculateValue(value1)))
Private Function CalculateValue(ByVal value1 As Integer) As Integer
If (value1 <= 0) Then
Throw New ArgumentOutOfRangeException(“Parameter cannot be less than 1!”)
Return CInt(100 / value1)
*Note: Comments are welcome here.