Understand why I am getting a "Specified cast is not valid" error

G

Guest

I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something along the lines of did you check this, have you did this, or this is the wrong way of doing this. I can access the CColPositions and the CPosition class, but when I try to use the two classes in a For...Next loop I get the error message above. Should I rebuild my VB6DLLFile and reference it in my project

In the Page_Load Event I have
Dim _posCollection As New CColPosition
Dim _pos As New CPositio
Dim strSSN As Strin
_posCollection.Retrieve(strSSN
For Each _pos In _posCollection '<----I get the error at this lin
Response.Write(_pos.PrimaryJobTitle
Nex

--------CColPositions-------
Namespace Employe
Public Class CColPosition
Inherits VB6DLLFile.colPositionsClas
End Clas
End Namespac

--------CPosition Class-------
Namespace Employe
Public Class CPositio
Inherits VB6DLLFile.clsPositionClas
End Clas
End Namespac

Thanks
Charle
 
K

Kevin Spencer

Please post the line of code that the Exception refers to, or indicate which
line of code is in the code you did post.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Charles said:
I do not understand why I am getting a "Specified cast is not valid"
error, since it has worked before. Something has changed and I am not
really sure what it could be. I am looking for something along the lines of
did you check this, have you did this, or this is the wrong way of doing
this. I can access the CColPositions and the CPosition class, but when I
try to use the two classes in a For...Next loop I get the error message
above. Should I rebuild my VB6DLLFile and reference it in my project?
 
G

Guest

Hi, Bi

Thanks for the reply

I did forget to say that it used to work using For...Next loops at one time too. The VB6DLLFiles do have some code in the classes so that For...Next can be used. So I am not real sure if System.IEnumerable will work out. But that is a tomorrow morning project since it is 5:15pm here

Thanks again
Charle


----- Bin Song, MCP wrote: ----

Hi, Charles

The collection class must implements System.IEnumerable in order to be enumerated

Bin Song, MCP
 
R

Rick Spiewak

I would put a breakpoint on the problem line, and see what the type makeup
of your collection is. You might be better off iterating with a counter, and
doing a ctype or directcast if the type is actually appropriate. If it
isn't, try re-defining your collection class based on
system.collections.collectionbase, and override the Add (etc.) methods for
strong typing.

Charles said:
I do not understand why I am getting a "Specified cast is not valid"
error, since it has worked before. Something has changed and I am not
really sure what it could be. I am looking for something along the lines of
did you check this, have you did this, or this is the wrong way of doing
this. I can access the CColPositions and the CPosition class, but when I
try to use the two classes in a For...Next loop I get the error message
above. Should I rebuild my VB6DLLFile and reference it in my project?
 
K

Kevin Spencer

Sorry, I missed your note.

Without seeing the code that creates the Collection class, it's hard to say
for sure.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
G

Guest

Hi, Charles

Do you mean it works in .NET or VB6? I can see that you interop the VB6 dll
What's the code in the classes so that For...Next can be used

Bin Song, MCP
 
G

Guest

It used to work in .NET. It still works in VB6

The code in the classes so that For...Next can be used is (VB6)
Public Property Get NewEnum() As IUnknow

'this property allows you to enumerat
'this collection with the For...Each synta
Set NewEnum = mCol.[_NewEnum

End Propert

Charle

----- Bin Song, MCP wrote: ----

Hi, Charles

Do you mean it works in .NET or VB6? I can see that you interop the VB6 dll
What's the code in the classes so that For...Next can be used

Bin Song, MCP
 
G

Guest

Thanks for everyones help. I decide just to access the VB6DLLFile directly. Hopefully I will not have other problems doing this.

Thanks again
Charle


----- Charles wrote: ----

I have found that if I access the VB6DLLFile directly, the For...Next loop does work. However, I would like to access the VB6DLLFile though a .NET project that will hold all the classes that come from VB6DLLFile (using Inherits) and new classes

Thanks
Charle


----- Charles wrote: ----

I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something along the lines of did you check this, have you did this, or this is the wrong way of doing this. I can access the CColPositions and the CPosition class, but when I try to use the two classes in a For...Next loop I get the error message above. Should I rebuild my VB6DLLFile and reference it in my project

In the Page_Load Event I have
Dim _posCollection As New CColPosition
Dim _pos As New CPositio
Dim strSSN As Strin
_posCollection.Retrieve(strSSN
For Each _pos In _posCollection '<----I get the error at this lin
Response.Write(_pos.PrimaryJobTitle
Nex

--------CColPositions-------
Namespace Employe
Public Class CColPosition
Inherits VB6DLLFile.colPositionsClas
End Clas
End Namespac

--------CPosition Class-------
Namespace Employe
Public Class CPositio
Inherits VB6DLLFile.clsPositionClas
End Clas
End Namespac

Thanks
Charle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top