473,386 Members | 1,775 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,386 software developers and data experts.

weird InvalidCastException

Here's my dilemma. I have a listbox that the user can select an item
("OrderItem" class) from, provided it hasn't already been selected (in
which case it will appear on a second list box). The following
procedure checks list2 (passed as lstSearch). It works fine the first
time through, but if the user attempts to add a second item, the
function crashes - throwing an Invalid castException on the indicated
line, saying that "Cast from type 'OrderItem' to type 'String' is not
valid."...despite the fact that it will work fine the first time
through!

Private Function OnList(ByVal strSearch As String, ByVal lstSearch As
ListBox) As Boolean

'Declare variables
Dim blnFound As Boolean
Dim intListIndex As Integer = 0

Do Until blnFound Or intListIndex = lstSearch.Items.Count
*If strSearch = CStr(lstSearch.Items(intListIndex)) Then*
blnFound = True
End If
intListIndex += 1
Loop
Return blnFound
End Function

Any thoughts?
Nov 20 '05 #1
3 927
Hi Larry,

I can look what gives the error, however for this routine I think that it is
more eaysy to use the method listbox findstringexact (there is also a method
findstring)

http://msdn.microsoft.com/library/de...exacttopic.asp

I hope this helps?

Cor
Nov 20 '05 #2
While I can't swear that I tried the findstring, I know I was having
problems originally because the list items are objects (passed using
the ToString function)and not straight text (or strings, for that
matter).

The thing that's bothering me is that if the user removes the item
from list2 (doubleclicking list2 items removes them), the boolean
serach will work again. So I can use it as often as I want, as long
as there's nothing currently in list2.

BTW, I tried changing the code to display the "Already selected"
message box on True instead of False and it gave me the message box,
so I know that the search is working and returning the false. Plus,
it's code I copied over from another working program in which list2 is
compared against a text entry. If any of that helps and.or makes
sense.

"Cor Ligthert" <no**********@planet.nl> wrote in message news:<eB**************@TK2MSFTNGP11.phx.gbl>...
Hi Larry,

I can look what gives the error, however for this routine I think that it is
more eaysy to use the method listbox findstringexact (there is also a method
findstring)

http://msdn.microsoft.com/library/de...exacttopic.asp

I hope this helps?

Cor

Nov 20 '05 #3
Hi Larry,

I think I get it, can you try this

*If strSearch =
Directcast(lstSearch.Items(intListIndex),OrderItem ).toString Then*

I hope this helps?

Cor
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
1
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
4
by: Ewald Hofman | last post by:
I have a very strange typing problem: There is the following class Public Class MyBaseClass Implents MyInterface Public Property MyProperty As MyClass implements MyInterface.MyProperty ......
1
by: Armond VanHeusen | last post by:
I have an application with a bunch of DLLs that are loaded in as plugins. In one of these I am trying to get XMLSerialization to work but with little success. I then tried some very simple code...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
0
by: drjack | last post by:
Recently I developed a website using asp.net and SQL Express. The database file is uploaded into App_Data folder. The website is using two membership providers, one is SqlMemership provider with...
8
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.