Solved

Form giving "specified cast is not valid" in call expecting object

Posted on 2006-11-16
1
247 Views
Last Modified: 2010-04-23
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_PROPERTYGET OrElse _
                               TLI.InvokeKinds.INVOKE_PROPERTYPUT OrElse _
                               TLI.InvokeKinds.INVOKE_PROPERTYPUTREF
                                    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_PROPERTYGET

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.


0
Comment
Question by:RegProctor
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 17955397
Sounds like you could be using System.Reflection calls instead:

Imports System.Reflection

...

        Dim prop As PropertyInfo = Me.GetType().GetProperty("name")

Bob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with preventing downloading a zip file 10 49
Optimize the query 5 50
VB.Net Determine if a mapped network drive exists 2 54
Need help with a query 14 36
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question