By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,361 Members | 3,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,361 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
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 CColPositions
Dim _pos As New CPosition
Dim strSSN As String
_posCollection.Retrieve(strSSN)
For Each _pos In _posCollection '<----I get the error at this line
Response.Write(_pos.PrimaryJobTitle)
Next

--------CColPositions--------
Namespace Employee
Public Class CColPositions
Inherits VB6DLLFile.colPositionsClass
End Class
End Namespace

--------CPosition Class--------
Namespace Employee
Public Class CPosition
Inherits VB6DLLFile.clsPositionClass
End Class
End Namespace

Thanks!
Charles

Nov 18 '05 #2

P: n/a
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
Nov 18 '05 #3

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
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 CColPositions
Dim _pos As New CPosition
Dim strSSN As String
_posCollection.Retrieve(strSSN)
For Each _pos In _posCollection '<----I get the error at this line
Response.Write(_pos.PrimaryJobTitle)
Next

--------CColPositions--------
Namespace Employee
Public Class CColPositions
Inherits VB6DLLFile.colPositionsClass
End Class
End Namespace

--------CPosition Class--------
Namespace Employee
Public Class CPosition
Inherits VB6DLLFile.clsPositionClass
End Class
End Namespace

Thanks!
Charles

Nov 18 '05 #4

P: n/a
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.

"Charles" <an*******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
I indicated which line of code that I got the exception on by this line:

For Each _pos In _posCollection '<----I get the error at this line

Thanks for the reply!
Charles

----- Kevin Spencer wrote: -----

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" <an*******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
> 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 CColPositions
> Dim _pos As New CPosition
> Dim strSSN As String
> _posCollection.Retrieve(strSSN)
> For Each _pos In _posCollection '<----I get the error at this line > Response.Write(_pos.PrimaryJobTitle)
> Next
>> --------CColPositions--------

> Namespace Employee
> Public Class CColPositions
> Inherits VB6DLLFile.colPositionsClass
> End Class
> End Namespace
>> --------CPosition Class--------

> Namespace Employee
> Public Class CPosition
> Inherits VB6DLLFile.clsPositionClass
> End Class
> End Namespace
>> Thanks!

> Charles
>

Nov 18 '05 #5

P: n/a
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
Nov 18 '05 #6

P: n/a
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
Nov 18 '05 #7

P: n/a
Hi, Charles

This article might be helpful. It mentioned to implement GetEnumerator() to get rid of For..Each error
http://www.standardio.org/article.aspx?id=20

Bin Song, MCP
Nov 18 '05 #8

P: n/a
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

Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.