Solved

code review request for Exists function

Posted on 2006-11-27
8
191 Views
Last Modified: 2010-04-23
Looking for some advice.  Can I tighten this up at all?

I don't want to waste time or do things I don't need to be doing

sourcesPassedIn looks like this:   "mdb","class","xxx","yyy", etc.

Sources (which is an arraylist) will contain a subset of the list above



Public Function ExistsInSources(sourcesPassedIn as string()) As Boolean

      Dim found as Boolean = false    
 
      for each s as String in sourcesPassedIn
        if Sources.Contains(s) then
          found = true
          return found
        End If
      Next
     
      return found

  End Function
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18023628
Hi knowlton,

a minor enhancement:

Public Function ExistsInSources(sourcesPassedIn as string()) As Boolean
 
      for each s as String in sourcesPassedIn
        if Sources.Contains(s) then
          return true
        End If
      Next
     
      return false

  End Function

Cheers,
Hillwaaa
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 18023634
If I sort the string array and the arraylist before looping, that would speed it up, I think.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 18023644
Hillwaaa:

Excellent.  Yes, no need to declare the boolean "flag" seperately!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 18023706
maybe if I passed in sourcesPassedIn as a hashtable instead of a string array and tried to just see if the hashtable contains Source(i).  If it does, then return true.

A hashtable is faster than a string array for lookups, isn't it?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 18023737
If I add 10000 strings as keys to a hashtable, versus having an array of 10000 strings, and I am looking for something towards the end of the list, the hashtable will find it much faster, right?

Will the hashtable perform faster if I sort it before looking for a certain Key?
0
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 500 total points
ID: 18023789
Hi knowlton - it will in part depend on how much data you have in the arraylist and if it is inserted into the list in order.

For an in depth article on this sort of issue, look at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/datastructures_guide.asp which starts with discussions on arrays and Arraylists, but also discusses Hashtables etc.

As for using a hashtable, I don't think (for what you are doing) that the hashtable will be faster - from the article "If you plan on searching the array by a specific unique key, it is much more efficient to use a Hashtable instead, as the lookups by key value occur in constant time as opposed to linear time."

However you will not be using a unique key to look it up, but just doing a search (iteration) until you find it.  

Maybe look at the Binary Search Tree in the above article if you have a large number of strings to look through...

Cheers,
Hillwaaa
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 18023887
Thanks
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18032191
I know this is closed, but....

are you using VS2005?
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question