Question

I'm trying to setup Code Contracts in a VB.NET project.

Here's a real simple method that should ensure that the passed in divisor argument is not zero:

Public Function Divide(ByVal numerator As Integer, ByVal divisor As Integer) As Double
    Contract.Requires(Of ArgumentOutOfRangeException)(divisor <> 0, "Divide By Zero Not Allowed")
    Return numerator / divisor
End Function

If I call Divide(5, 0), I should get a compile time error. But alas, I don't:

demo

I've downloaded the Add-In from the Visual Studio Gallery.
I have static code analysis enabled on the Code Contracts Property Page:

PropertyPage

This works when I follow the same steps in C#:

working

What else could be missing?

No correct solution

OTHER TIPS

I am not familiar with "CodeContracts" but I will give this a try.

It may be because you are Dim'ing something, but are dividing it in the same line, so "CodeContracts" may think that, "Oh, this line is Dim'ing something, move on to next line."

By this I mean, that the "CodeContract" is looking for an actual line of code that is just dividing. For example:

result = 5 / 0

Instead of:

result = Divide(5, 0)

This could also just be, the technique in which you are dividing, but keep in mind, I have never worked with "CodeContracts" before, so this may be way off, but hopefully this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top