1

There are three parts to my code A, B, C. B is a class library which references A which is also a class library. In A, a class MathOperations is defined which contains a function ADD. The part B has a class Factory which in-turn has a function createMathsObject which returns an object of MathOperations type. The part C refers to B. I can do away with part B and create the object of MathOperations type in part C and make C reference A directly where that class is defined. However I am trying to see how I can de-couple sections of the code, in this case A and C.I give the code for each of the three parts A, B and C. The error message that I get is mentioned later for which I need your help.

Part A: Class Library - Named as MathLibrary.dll

Public Class MathOperations
    Function ADD(ByVal a As Integer, ByVal b As Integer) As Integer
        Return (a + b)
    End Function
End Class

Part B: Class Library - Named as FactoryMathsLibrary.dll which references MathLibrary.dll

Imports MathLibrary
Public Class Factory
    Shared Function createMathsObject() As MathOperations
        Return New MathOperations()
    End Function
End Class

Part C: Exe file - which references FactoryMathsLibrary.dll

Imports FactoryMathsLibrary
Module Module1
    Sub Main()
        Dim a, b As Integer
        Dim m As Object = Factory.createMathsObject() 'Error comes here
        Console.Write("Enter 1st value: ")
        a = Console.ReadLine()
        Console.Write("Enter 2nd value: ")
        b = Console.ReadLine()
        Console.WriteLine("a + b = {0}", m.ADD(a, b))
        Console.ReadLine()
    End Sub
End Module

I was under the impression that since C references B and B references A, everything would be fine. But I am getting an error in the line Dim m As Object = Factory.createMathsObject(). The error message is as follows: Can someone pls help with the reason behind the error and the solution??

Error message

Sougata
  • 319
  • 1
  • 10

1 Answers1

2

I did exactly what you did, in your exe-project you haven't added reference to "MathLibrary.dll". You can't include a library that references to another library without including that second library in your exe-project too (Unless you merge them into one)! if you won't include it too, you will have exactly the issue that you are having.

And so, you only have to add reference to "FactoryMathsLibrary.dll" and to "MathLibrary.dll" Project > project_name Properties... > References > Add... > MathLibrary.dll > OK

❌ Without reference

enter image description here

✔️ With reference

enter image description here

Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32