RegProctor
asked on
Form giving "specified cast is not valid" in call expecting object
I am adding a property to some of my forms and would like to be able to check for it's existence without using error trapping (just trying to be more elegant).
I found this nice code for checking the existence of a property of an object here at EE and so implemented it (with a little customization naturally).
However, as you can see with the example calls below, it works for controls but not forms.
Does anyone know of an elegant way of doing this with forms or should I just use do something basic like use error trapping or keep a hard coded list of the forms that I want to track as having certain characteristics.
Also, I am just starting with vb.net (got years of VB2-6 though) and my understanding was that forms in vb.net were regarded as true forms/objects (in the old VB they were fudged a bit in some respects) so I am little puzzled as to why this does not work with forms, any enlightenment would be appreciated.
ret = HasProp(Myform.MyControl, "MyProperty") ' This works
ret = HasProp(Myform, "MyProperty") ' This does not work
Public Function HasProp(ByRef obj As Object, ByVal sProp$) As Boolean
Dim prop As TLI.MemberInfo
Dim TL As TLI.TLIApplication
Dim oInterface As TLI.InterfaceInfo
TL = New TLI.TLIApplication
oInterface = TL.InterfaceInfoFromObject (obj) ' This is the line that gives the error: "specified cast is not valid"
For Each prop In oInterface.Members
If UCase(prop.Name) = UCase(sProp) Then
Select Case prop.InvokeKind
Case _
TLI.InvokeKinds.INVOKE_PRO PERTYGET OrElse _
TLI.InvokeKinds.INVOKE_PRO PERTYPUT OrElse _
TLI.InvokeKinds.INVOKE_PRO PERTYPUTRE F
Return True
End Select
End If
Next
Return False
end function
One other question. In all the examples of this code on EE I saw something like:
Case INVOKE_PROPERTYGET
That would not work for me and I had to fully qualify it as:
Case TLI.InvokeKinds.INVOKE_PRO PERTYGET
If there is something I missing on being able to use shorter qualifying I would appreciate knowing how. I did try using "with TLI.InvokeKinds" but that was not allowed, as I suspected would be the case.
I found this nice code for checking the existence of a property of an object here at EE and so implemented it (with a little customization naturally).
However, as you can see with the example calls below, it works for controls but not forms.
Does anyone know of an elegant way of doing this with forms or should I just use do something basic like use error trapping or keep a hard coded list of the forms that I want to track as having certain characteristics.
Also, I am just starting with vb.net (got years of VB2-6 though) and my understanding was that forms in vb.net were regarded as true forms/objects (in the old VB they were fudged a bit in some respects) so I am little puzzled as to why this does not work with forms, any enlightenment would be appreciated.
ret = HasProp(Myform.MyControl, "MyProperty") ' This works
ret = HasProp(Myform, "MyProperty") ' This does not work
Public Function HasProp(ByRef obj As Object, ByVal sProp$) As Boolean
Dim prop As TLI.MemberInfo
Dim TL As TLI.TLIApplication
Dim oInterface As TLI.InterfaceInfo
TL = New TLI.TLIApplication
oInterface = TL.InterfaceInfoFromObject
For Each prop In oInterface.Members
If UCase(prop.Name) = UCase(sProp) Then
Select Case prop.InvokeKind
Case _
TLI.InvokeKinds.INVOKE_PRO
TLI.InvokeKinds.INVOKE_PRO
TLI.InvokeKinds.INVOKE_PRO
Return True
End Select
End If
Next
Return False
end function
One other question. In all the examples of this code on EE I saw something like:
Case INVOKE_PROPERTYGET
That would not work for me and I had to fully qualify it as:
Case TLI.InvokeKinds.INVOKE_PRO
If there is something I missing on being able to use shorter qualifying I would appreciate knowing how. I did try using "with TLI.InvokeKinds" but that was not allowed, as I suspected would be the case.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.