Solved

code review request for Exists function

Posted on 2006-11-27
8
186 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:knowlton
  • 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: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:knowlton
ID: 18023644
Hillwaaa:

Excellent.  Yes, no need to declare the boolean "flag" seperately!
0
 
LVL 5

Author Comment

by: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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by: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: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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now