473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
8 1775
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Özden Irmak | last post by:
Hello, In my application, I've to cast DocumentDesigner class to my own derived class,MyDocumentDesigner, in order to reach the protected properties in DocumentDesigner class. But everytime I...
5
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
3
by: .Net Sports | last post by:
IN a datagrid code behind, I'm getting a "Specified cast is not valid" error price = (Decimal)(rowData); which was initialized as: Decimal price;
1
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
3
by: Jayyde | last post by:
Not sure why all of a sudden this line of code: cmbProductCategoryTreeDisplayName.SelectedIndex = 0; is producing a "Specified cast is not valid error". In the watch it has 1 item in it at...
3
by: keithb | last post by:
Using a GridView, I get a "Specified cast is not valid" error when binding the Visible propery of a hyperlink control to a DataTable text field. The error goes away if I replace the data binding...
2
by: vijayasingh | last post by:
I am trying to take contacts data from out look and Synchronize with our database. I am using .net 1.1 framework. iam using following code. OLCont = MyOLList.GetLast 'CType(item,...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.