8

I found C# code for it here

So I tried

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetMethod(propertyy)
End Function

But it throws an error at type.GetMethod(propertyy) saying "Value of type 'System.Reflection.MethodInfo' cannot be converted to 'Boolean'."

What to do?

4 Answers 4

21

First, C# code checks for presence of a method, not a property. Second, C# code compares return to null:

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetProperty(propertyy) IsNot Nothing
End Function

EDIT To check for fields, change the method as follows:

Public Function checkField(ByVal objectt As Object, ByVal fieldName As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetField(fieldName) IsNot Nothing
End Function
2
  • Return type.GetProperty(propertyy) IsNot Nothing is always returning False, even if the property definitely does exist. I tried checkProperty(test, "id") and checkProperty(test, "test") . The former should be true, but isn't. MsgBox(type.Name) correctly indicates that the Class found is of type "CTest". CTest has two properties: Public id as integer and Public name as string
    – natli
    Feb 22, 2012 at 17:37
  • 1
    @natli Both id and name are instance variables, not properties. See my update. Feb 22, 2012 at 17:43
5

it is returning the MethodInfo instead and you can just change it as follow:

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetMethod(propertyy) IsNot Nothing
End Function
0

You're trying to return type.GetMethod(propertyy), where your example code is returning the result of evaluating if that method is null or not.

try Return type.GetMethod(propertyy) isnot nothing

0

By splitting the line:

        Return type.GetMethod(propertyy) IsNot Nothing

of dasblinkenlight into the three lines;

        Dim info As System.Reflection.PropertyInfo = type.GetProperty(propertyy)
        Dim reallyExists As Boolean = info IsNot Nothing
        Return reallyExists

the function checkProperty will return true on existing properties.

I cannot comment to the answer of dasblinkenlight, since my reputation is still less than 50, so send my above improvement on his answer as a new answer. I am not happy with this, since it makes the pages on stackoverflow less readable. In order to make this a true answer I include a module I created, in which the lines

Public Function propertyExists( _

through

End Function

are improvements to the code of function checkProperty of natli and dasblinkenlight.

See Verify that an object has a certain property for the question of natli, that I am answering to. See https://stackoverflow.com/posts/9399928/revisions and the question of natli for the answer of dasblinkenlight, that I am commenting to.

By the way: "As you will see below I prefer not to use system namespaces, so that I can directly see what system functions are referenced."

The module with the name net2Module I created to use this follows;

''' <summary>
''' This module with the name net2Module contains
''' tools that need at least .NET Framework 2.0.
''' This module needs System.Reflection.
''' </summary>
Public Module net2Module

''' <summary>
''' If one of the tools fails, the property exceptionMessage will
''' not be nothing, but contain an exception.
''' Each tool will set exceptionMessage to nothing or an exception.
''' </summary>
Public exceptionMessage As System.Exception = Nothing

''' <summary>
''' Checks if a property with some name exist in an object.
''' This function needs System.Reflection.
''' </summary>
''' <param name="objectt">The object.</param>
''' <param name="propertyy">The name of the property.</param>
''' <returns>True if the property exists.</returns>
Public Function propertyExists( _
        objectt As Object, _
        ByVal propertyy As String _
        ) As Boolean
    Try
        exceptionMessage = Nothing
        Dim type As System.Type = objectt.GetType
        Dim info As System.Reflection.PropertyInfo = _
            type.GetProperty(propertyy)
        Dim reallyExists As Boolean = info IsNot Nothing
        Return reallyExists
    Catch ex As System.Exception
        exceptionMessage = ex
        Return False
    End Try
End Function ' propertyExists
End Module ' net2Module

In the following code I succesfully use my function propertyExists in order to close or hide child forms recursively;

Friend Module sharedEnums
Friend Enum objectNamesEnum
    formHandlingClass
    calledForms
    ownedForms
End Enum ' objectNamesEnum

Friend Enum recursiveFormTypesEnum
    calledForms
    ownedForms
End Enum ' recursiveFormTypesEnum

Friend Enum recursiveActionsEnum
    hideForms
    closeForms
End Enum ' recursiveActionsEnum
End Module ' sharedEnums

Friend Class recursiveClass

Friend recursiveFormTypes As New recursiveFormTypesEnum

Friend recursiveActions As New recursiveActionsEnum

Friend Sub hideOrCloseFormsRecursively( _
        formsToHandle As System.Windows.Forms.Form())
    If Not formsToHandle Is Nothing Then
        Dim formToHandle As System.Windows.Forms.Form = Nothing
        Dim propertyToExist As String = String.Empty
        If Me.recursiveFormTypes = recursiveFormTypesEnum.calledForms Then
            propertyToExist = objectNamesEnum.calledForms.ToString
        Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
            propertyToExist = objectNamesEnum.ownedForms.ToString
        End If
        For Each formToHandle In formsToHandle
            Try ' Recurse through the forms to handle
                Dim formObject As Object = formToHandle
                If net2Module.propertyExists(formObject, _
                        objectNamesEnum.formHandlingClass.ToString) Then
                    If net2Module.propertyExists( _
                            formObject.formHandlingClass, propertyToExist) Then
                        If Me.recursiveFormTypes = _
                                recursiveFormTypesEnum.calledForms Then
                            Call Me.hideOrCloseFormsRecursively( _
                                formObject.formHandlingClass.calledForms.ToArray)
                        Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
                            Call Me.hideOrCloseFormsRecursively( _
                                formObject.formHandlingClass.ownedForms)
                        End If
                    End If
                End If
                If net2Module.propertyExists(formObject, propertyToExist) Then
                    If Me.recursiveFormTypes = _
                            recursiveFormTypesEnum.calledForms Then
                        Call Me.hideOrCloseFormsRecursively( _
                            formObject.calledForms.ToArray)
                    Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
                        Call Me.hideOrCloseFormsRecursively( _
                            formObject.ownedForms)
                    End If
                End If
            Catch
            End Try
            Try ' Take the action to take on each found form
                If Me.recursiveActions = _
                        recursiveActionsEnum.hideForms Then
                    Call formToHandle.Hide()
                Else ' Me.recursiveActions = recursiveActionsEnum.closeForms
                    Call formToHandle.Close()
                End If
            Catch
            End Try
        Next
    End If
End Sub ' hideOrCloseFormsRecursively

End Class ' recursiveClass

I would be delighted to read from you if you have been helped by me or not. I am Dutch, so would also like comments on my Engish, so that I can improve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.