Scott Savage
Scott Savage

Reputation: 373

How do i pass a class as an argument and then use a shared method of that class?

NOTE: This question is NOT asking how to pass an instance of a class. It is asking how to pass the class which has shared items.

For example ... Any idea on how to make a function that will do this?

Public Function InsertSQL(ByVal xClass As Type) As String
    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & xClass.TableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & xClass.FieldList
    InsertSQL = InsertSQL & " )"
End Function

With a set of classes like this:

Public Class cClient
    Public Shared TableName = "Clients"
    Public Shared Function FieldList() As String
        FieldList = "Name, Phone"
    End Function
End Class

Public Class cSale
    Public Shared TableName = "Sales"
    Public Shared Function FieldList() As String
        FieldList = "Name, Amount"
    End Function
End Class

I have search for "How to a pass a class as an argument" on Google, but have not found anything that addresses a way to access shared methods of the class. I can only get results that think I am wanting to pass an instance of that class.

Note that this code is just an example. And - of course, I have no problem if I pass an instance of a class.
But - that is not what I am trying to figure out.

Upvotes: 1

Views: 2071

Answers (1)

Steve
Steve

Reputation: 216243

I would use Inheritance in your situation. Something like this

Public Sub Main
    Dim c = new cClient()
    Dim sql = InsertSQL(c)
    Console.WriteLine(sql)
End Sub


' Pass an actual instance of a class that inehrits cTableBase'
' all of these classes have the same BASE functions and property
' defined in the base class
Public Function InsertSQL(ByVal xClass As cTableBase) As String
    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & xClass.TableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & xClass.FieldList
    InsertSQL = InsertSQL & " )"
End Function

Public Class cTableBase
    Public TableName As String 
    Public Overridable Function FieldList() As String
    End Function
End Class

Public Class cClient 
    Inherits cTableBase
    Public Sub New
        TableName = "Clients"
    End Sub
    Public Overrides Function FieldList() As String
        FieldList = "Name, Phone"
    End Function
End Class

Public Class cSale 
    Inherits cTableBase
    Public Sub New
        TableName = "Sales"
    End Sub
    Public Overrides Function FieldList() As String
        FieldList = "Name, Amount"
    End Function
End Class

As you have posted in the comment below. This could be done using the Type passed to the method InsertSQL using reflection

Sub Main
    Dim c = new cClient()
    Dim result = InsertSQL(c.GetType())
    Console.WriteLine(result)
End Sub

Public Function InsertSQL(ByVal xClass As Type) As String

    Dim fi = xClass.GetField("TableName")
    Dim tableName = fi.GetValue(xClass)

    Dim mi = xClass.GetMethod("FieldList")
    Dim fieldList = mi.Invoke(xClass, Nothing)

    InsertSQL = "Insert into "
    InsertSQL = InsertSQL & tableName
    InsertSQL = InsertSQL & " ("
    InsertSQL = InsertSQL & fieldList
    InsertSQL = InsertSQL & " )"
End Function

Upvotes: 3

Related Questions