passing a class as an argument

Hi I wish to pass a class as an argument so I can search a collection and count the number of instances of that type, but I am unsure of the correct way todo this. Curremtly i have this.

public Function CountType(aType As Type, col As Collection) As Integer
dim intCount  = 0
for each aObject As Object in col
                If aObject.GetType.Equals(aType) Then
                    intCount += 1
                End If
next
return intCount  
end function

this seems to work but I have to pass a class using GetType(MyClass) rather than just passing MyClass

can anyone suggest a cleaner way todo this

thankyou


Answer this question

passing a class as an argument

  • vivek.bhoj

    Thanks for the reply , I thought there may be a cleaner solution because I noticed the Ctype function takes a Type as an argument  

    CType(Object as expression, Object as Type) as Type
    hence allowing this 
    CType(aObject, String)

    When Type is an argument parameter it allows  'String' to be passed as the argument


    but in my original code in the first post, when i use Type as an argument  parameter I have to pass getType(String)

    I guess a built in function is quite different from a user defined method... 

  • Sauas

    The way you are doing is just fine


    you would want this function to work on any type, so if you were passing in a class, that would make your function less reusable, and the name of the function invalid.

    what you got here is very clean

  • G Pearlman

    any comment would be nice, such as this is the way todo it, or hey you understand nothing :)
  • passing a class as an argument