0

The assert statement in the code below does not function properly. How can I control the assert with another variable decision? I am new in coding.

    <TestClass>
    Public Class FactorsOf32DictionaryCatchError
        <TestMethod>
        Sub TestSub()
            Dim FactorsOf32 As New Dictionary(Of Integer, Integer)
            FactorsOf32.Add(1, 32)
            FactorsOf32.Add(2, 16)
            FactorsOf32.Add(4, 8)
            FactorsOf32.Add(32, 1)
            FactorsOf32.Add(16, 2)
            FactorsOf32.Add(8, 4)
            Dim ProductOf32 As Integer
            Dim i As Integer = 0
            For i = 1 To 32
                Try
                    ProductOf32 = FactorsOf32.Item(i) * i

                Catch
                    Debug.Print(String.Format("Error is contained in the operation"))
                End Try
                Assert.AreEqual(ProductOf32, 32, "The result did not contain what you thought")
            Next i
        End Sub
    End Class
End Namespace
  • Please explain exactly what you expect to happen and exactly what does happen. Just saying that something doesn't work is not enough because all code does what it does so all code works from that perspective. It's the fact that the actuality doesn't match the expectation that is the problem but, if we don;t know what either of them are, we have to assume or guess. There's no reason that we should have to do that when you already know both. – jmcilhinney Feb 27 '20 at 05:10
  • I'd suggest looking at the documentation for the `Assert` module; there are other options than equality, and as a fallback, you can accomplish just about anything by asserting a boolean variable is either true or false. – Craig Feb 27 '20 at 15:53
  • @jimchinney and Craig. I created a dictionary where each factor of 32 is a key to its corresponding factor. I tried using a Try Catch to deal with not having a key in the dictionary. If the key is found, I intend to Assert that the key times the value of the current pair is 32. It still fails running it, How do I control the assert with another variable decision. Alternatively, how can I limit the catch to catch only the correct error? – Aderoju Ademola Feb 28 '20 at 01:48
  • `Catch` can be set to only catch particular types of exceptions. Beyond that, see my previous comment, have you looked at the documentation of `Assert`? – Craig Feb 28 '20 at 22:19

0 Answers0