?
Solved

code review request for Exists function

Posted on 2006-11-27
8
Medium Priority
?
194 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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