title: Create VBA class in VB.net
body: <p>Is it possible (using COM and regasm.exe) to have a vba function call a vb.net function - which creates the class in vb.net and then passes the class back to vba, where it is recognised as a vba class?</p>
  
  <p>In VBA, I can work with classes by using Insert>Class Module. I have set up a function that creates a class.</p>
  
  <pre><code>Private length As Double
  Private height As Double
  Public Sub init(ByRef hgt As Double)
    height = hgt
    length = dbl_height()
  End Sub
  
  Public Function dbl_height()
    dbl_height = height * 2
  End Function
  </code></pre>
  
  <p>I can initialize it accordingly using this function:</p>
  
  <pre><code>Public Function CreateClassFunction(foo As Integer)
    Dim my_rect As Rectangle
    Set my_rect = New Rectangle
    my_rect.init (foo)
    Set CreateClassFunction = my_rect
  End Function
  </code></pre>
  
  <p>I can also do the same thing in vb.net with virtually identical code.</p>
  
  <pre><code>Public Class Rectangle
     Private length As Double
     Private height As Double
     Public Sub init(ByRef hgt As Double)
       height = hgt
       length = dbl_height()
     End Sub
  
     Public Function dbl_height()
       dbl_height = height * 2
     End Function
  End Class
  </code></pre>
  
  <p>where this vb.net function creates the class:</p>
  
  <pre><code>Public Function CreateClassFunction(foo As Integer) As Rectangle
      Dim my_rect As Rectangle
      my_rect = New Rectangle
      my_rect.init(foo)
      CreateClassFunction = my_rect
  End Function
  </code></pre>
  
  <p>I can pull in a Variant/Object/Rectangle into vba using:</p>
  
  <pre><code>Function MyCreateClass(a As Double)
    Dim classLib As New MyAnalytics.Class1
    Set MyCreateClass = classLib.CreateClassFunction(a)
  End Function
  </code></pre>
  
  <p>However this object does not have the height or length variables. (It says "no variables" on the watch window)</p>
  
  <p>Edit:</p>
  
  <p>Amended code as per Mat's Mug answer:</p>
  
  <pre><code>Public Class Rectangle
  Private plength As Double
  Private pheight As Double
  Public Property length() As Double
      Get
          Return plength
      End Get
      Set(ByVal value As Double)
          plength = value
      End Set
  End Property
  
  Public Property height() As Double
      Get
          Return pheight
      End Get
      Set(ByVal value As Double)
          pheight = value
      End Set
  End Property
  
  Public Sub init(ByRef hgt As Double)
      height = hgt
      length = dbl_height()
  End Sub
  
  Public Function dbl_height()
      dbl_height = height * 2
  End Function
  
  End Class
  </code></pre>
  
  <p>and testing in VBA:</p>
  
  <pre><code>Function MyCreateClass(a As Double)
    Dim classLib As New MyAnalytics.Class1
  
    Set MyCreateClass = classLib.CreateClassFunction(a)
  
    Debug.Print MyCreateClass.Height()
    Debug.Print MyCreateClass.length()
  
    MyCreateClass.Height = 30
    MyCreateClass.length = 20
  
    Debug.Print MyCreateClass.Height()
    Debug.Print MyCreateClass.length()
  
    MyCreateClass.init (100)
    Debug.Print MyCreateClass.Height()
    Debug.Print MyCreateClass.length()
  
  
  End Function
  </code></pre>