invalid cast exception

C

cs_hart

I am getting an invalid cast exception - cast from string to type
double is not valid.

Dim curName As String
Dim prevName As String = ""
curName = CStr(rows.Item(i).Item(colSchName)) ' extract name from
database row
If (curName.CompareTo(prevName <> 0)) Then <--- exception occurs on
this line

I do a cast on line 3 but that is not the one getting an exception. Any
help appreciated...charlie
 
C

Cor Ligthert

CS,

Any reason that you do not just

\\\
If curName <> "" then
///

You make me curious

Cor
 
C

cs_hart

THis is the first time through. If these are not equal then I save it
in a list , assing curname to prevname and go to the next. I'm trying
to eliminate any duplicates during the processing
 
C

Cor Ligthert

CS,

What is than the difference with this sentence in my idea it is in this case
exactly the same

If (curName.CompareTo(prevName)) <> 0 Then

(With corrected error, in your code are you testing agains an evaluation)

It is good to set Option Strict On in your program, this will than be showed
direct when you are making your code.

Cor
 
B

Brian Swanson

You've got your not equal to zero check in the wrong place.

The line should read:

If (curName.CompareTo(prevName) <> 0) Then

That should fix your cast exception error.

Hope this helps,
Brian Swanson
 
G

Guest

Hi CS,

the closing braket is at the wrong place, so you compare prevName to 0

If (curName.CompareTo(prevName) <> 0)
^

Roland
 

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

Top