title: Decoupling: Unable to implement indirect referencing
body: <p>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 <code>MathOperations</code> is defined which contains a function <code>ADD</code>. The part B has a class <code>Factory</code> which in-turn has a function <code>createMathsObject</code> which returns an object of <code>MathOperations</code> type. The part C refers to B. I can do away with part B and create the object of <code>MathOperations</code> 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.</p>
  <p>Part A: Class Library - Named as <em>MathLibrary.dll</em></p>
  <pre><code>Public Class MathOperations
      Function ADD(ByVal a As Integer, ByVal b As Integer) As Integer
          Return (a + b)
      End Function
  End Class
  </code></pre>
  <p>Part B: Class Library - Named as <em>FactoryMathsLibrary.dll</em> which references <em>MathLibrary.dll</em></p>
  <pre><code>Imports MathLibrary
  Public Class Factory
      Shared Function createMathsObject() As MathOperations
          Return New MathOperations()
      End Function
  End Class
  </code></pre>
  <p>Part C: Exe file - which references <em>FactoryMathsLibrary.dll</em></p>
  <pre><code>Imports FactoryMathsLibrary
  Module Module1
      Sub Main()
          Dim a, b As Integer
          Dim m As Object = Factory.createMathsObject() 'Error comes here
          Console.Write(&quot;Enter 1st value: &quot;)
          a = Console.ReadLine()
          Console.Write(&quot;Enter 2nd value: &quot;)
          b = Console.ReadLine()
          Console.WriteLine(&quot;a + b = {0}&quot;, m.ADD(a, b))
          Console.ReadLine()
      End Sub
  End Module
  </code></pre>
  <p>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 <code>Dim m As Object = Factory.createMathsObject()</code>. The error message is as follows: Can someone pls help with the reason behind the error and the solution??</p>
  <p><a href="https://i.stack.imgur.com/JJW17.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JJW17.png" alt="Error message" /></a></p>