Search code examples
vb.netshared

Class.GetType() : Reference to a non-shared member requires an object reference in VB.net


I hope this is not a duplicate, here's a simplified sample of my code :

Public Class ClassUsingFriendClassComparerCustomCollection
  ' Blah blah members blah blah methods
  Private collection As ArrayList
  Public Sub sort() _
    Implements FNUICollection.sort
    Me.collection.Sort(New ComparerCustomCollection)
  End Sub
End Class

Friend Class ComparerCustomCollection
Implements IComparer
  Public Enum StuffCases
    GoForStuff
    GoForOtherStuff
  End Enum

  Private Function getCase(type as System.Type) As StuffCases
    ' Blah blah returns StuffCases value
  End Function

  Private Function relevantFunction(x as ACustomObject) as Boolean
  ' Select Case Me.getCase(ACustomObject.GetType()) ' Fails :(
    Select Case Me.getCase(Case x.GetType())        ' Works :)
      Case GoForStuff
        Me.doSomeStuffWith(x)
      Case GoForOtherStuff
        Me.doSomeOtherStuffWith(x)
    End Select
  End Function
End Class

I can't figure out why the commented line fails... Visual Studio reports :

Reference to a non-shared member requires an object reference in VB.net

Did I miss an obvious thing ?


Solution

  • It fails because ACustomObject is a type and x is an object of that type. Type.GetType(). In your case possible ways to use this will be like the following:

    x.GetType()
    

    Or

    Type.GetType("ACustomObject")