Solved

code review request for Exists function

Posted on 2006-11-27
8
187 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 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

23 Experts available now in Live!

Get 1:1 Help Now