Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Is this bad programming or am I just a Genius :p


Reply
 
Thread Tools Display Modes
  #1  
Old 04-20-2006, 06:56 PM
death_entry's Avatar
death_entry death_entry is offline
Centurion
 
Join Date: Mar 2005
Posts: 167
Default Is this bad programming or am I just a Genius :p


I have a while loop that incraments an arraylist which holds index locations to a second array, however during this while loop an element may be removed from a second arralist and the program will crash as the element is not longer there, so I have used an exception handler which then removes this element from the arraylist when it finds the element in the second arraylist is no longer there, thus allowing the while loop to skip it and then continue to the next element in the first arraylist.

Now is this bad programming practise by letting it generate an exception on purpose because I don't see anyway to get it to check to see if that element still exists. Man I wish I had used a hashtable but it's too late for that now

is there a way to check to see if an object exists in a hashtable by supplying it with a key.
Reply With Quote
  #2  
Old 04-20-2006, 07:25 PM
PrOpHeT's Avatar
PrOpHeT PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,054
Default

can you not usa a for each loop and only check the ones that do exist, or am I missing something?
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #3  
Old 04-20-2006, 08:28 PM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

You can use a combination of ArrayList.Contains and ArrayList.Count to determine if an ArrayList contains a specified object or if a specific index is out of range.

The Hashtable provides a ContainsKey method that's pretty self-explanatory

You should NEVER use exceptions for controlling application flow - it gets too expensive and it violates the idea of an exception. Exceptions should only occur in truly exceptional circumstances.
Reply With Quote
  #4  
Old 04-21-2006, 08:07 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

In other words, not a genius.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #5  
Old 04-21-2006, 08:53 AM
death_entry's Avatar
death_entry death_entry is offline
Centurion
 
Join Date: Mar 2005
Posts: 167
Default

I can't use a for loop as it doesn't like you deleting elements as it messes up the iteration. And I don't think the contains in the arraylist will work since the numbers change since they are index positions. Since what used to be 2 is now object 3, with a hastable it doesn't change.

Ok ok lol I didn't mean look at me I know something you don't, have you not ever thought of something and then thought I am quite proud for thinking that?
Reply With Quote
  #6  
Old 04-21-2006, 09:03 AM
anthony_n's Avatar
anthony_n anthony_n is offline
Senior Contributor
 
Join Date: May 2005
Location: Manchester,England
Posts: 1,293
Default

when I was first learning programming then yes. not so much these days
__________________
ADO TUTOR

File IO Tutor
Reply With Quote
  #7  
Old 04-21-2006, 09:15 AM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

If you're using a For loop and want to delete elements, step backwards through the collection/array...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->