Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

code review request for Exists function

Posted on 2006-11-27
8
Medium Priority
?
195 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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