How can you call a Shared Class Method from a VB6 application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbvbprgrmr
    New Member
    • Jan 2010
    • 1

    How can you call a Shared Class Method from a VB6 application?

    Is it possible to call a .NET shared class method from a VB6 application?

    I created a small test project consisting of a .NET class library containing the following class:

    Code:
    Public Class MyTest
    
        Sub New()
    
        End Sub
    
        Public Function AddNumbers(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer
    
            Return Num1 + Num2
    
        End Function
    
        Public Shared Function AddStrings(ByVal String1 As String, ByVal String2 As String) As String
    
            Return String.Concat(String1, String2)
    
        End Function
    
    End Class
    I compiled the class with the "Register for COM Interop" enabled.

    I created a new VB6 project and referenced the .NET class library. I put a button on the form and inside the button I put the following code:

    Code:
    Private Sub Command1_Click()
    
        Dim i As New MyTest
        MsgBox i.AddNumbers(1, 1)
        MsgBox i.AddStrings("Hello", "hello")
       
    End Sub
    The AddNumbers call works fine, and produces a message box with '2' while the AddStrings call fires the error 'Object doesn't support this property or method'.

    I realize calling a shared method doesn't require an instance of the class but I was not sure how to call a shared method in a VB6 app so in the example above, I treated it similar to an instance method, unfortunately, without success. I also tried calling it like a .NET shared method would be called IE: MyTest.AddStrin gs but the compiler complained about "MyTest" being an undeclared variable.

    Any suggestions? Has anyone tried this before?
Working...