Archived

This topic is now archived and is closed to further replies.

Big B

vb questions

Recommended Posts

I am trying to write my own hashtable for VB6 and have hit a couple of slight stumbling blocks. To generate the keys, I was going to sum the values of the string and go from there. Except it doesn''t like this:
  
Function Hash_Key(name As String) As Integer
    Dim result, i As Integer
    result = 0
    For i = 1 To Len(name)
        result = result + name(i)
    Next i
    Hash_Key = result
End Function
  
as name isn''t an array. Is there any way to treat it as an array or just get the ith character? The second problem I have a work around for, but it troubles me. There doesn''t seem to be a way in VB to test if an array element has a value or not. If you access an element that doesn''t have a value, then an error is thrown and you catch it.
  
Sub Hash_Put(name As String, info As StringSet)
    On Error GoTo ERRH
    Dim index As Integer
    Dim testSet As StringSet
    Dim done As Boolean
    done = False
    index = Hash_Key(name) Mod hash_size
    Do While Not done
        testSet = Titles(index)
        index = index + 1
        If index >= hash_size Then
            done = True
        End If
    Loop
ERRH:
    Titles(index) = info
    MsgBox "Hash_put:" & index
End Sub
  
This seems to me to be a bad way of doing things, so if anyone has a more non-hackish way of determining if an array element is being used or not, please post it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
1.
You could use
result = result + mid(name,i,1)
in plase of
result = result + name(i)

2.
No I dont think you can.
Try makeing your own list class to solve this. Or use a Collection as the base.

Share this post


Link to post
Share on other sites
Thanks AP, but

result = result + mid(name,i,1)

didnt work, but

result = result + Asc(Mid(name, i, 1))

did.

The second problem isn''t such a problem, as all the array elements have a value, with all values set to "".

Share this post


Link to post
Share on other sites
I believe you can use ''x Is Nothing'' or ''x Is Empty'' (I forget which - VB have three different values that all mean ''this doesn''t have anything in it'') to test if something has a value assigned.

All your bases belong to us

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
1. I never said that was the exact code but just an over all idea.
Another thing is Name is a Unicode string this might be a problem if you are not careful.

2.Yes but VB protects the programmer that is why you are getting an error raised. If you make your own list class with a search key you would be a little safer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think an ‘if Titles(index) is nothing’ might work. It depends on what type the array is, but I don’t know.

Share this post


Link to post
Share on other sites
This should work (note the new declaration line, you were declaring Result as a Variant). Also, I switched things from Integers to Longs because longs are faster . Another thing which wasn''t mentioned is that I used Mid$ not Mid. This means it returns a string not a variant.


Function Hash_Key(Name As String) As Long
Dim Result As Long, i As Long
result = 0

For i = 1 To Len(name)
Result = Result + Asc(Mid$(Name, i, 1))
Next i

Hash_Key = Result
End Function


For checking if an array element exists, use UBound and LBound functions (or for more clear code, always have arrays zero based and skip the LBound function). Unfortunately, there is no easy way to test the number of dimensions an array has, so you need to keep track of that yourself and if you try to get the size of a null array, there will be an error.

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
And those special values (Nothing, Empty and Null) only work for some data types. Nothing is a special object reference (similar to a pointer to null in C++). Empty and Null are both for Variants, and unless something is wrong, you shouldn''t be using variants

Why do I keep bashing Variants? Take the following two pieces of code:

MySingle = Int(MySingle) ''Truncate MySingle

OR:

MySingle = MySingle \ 1 ''Integer divide MySingle by 1

Which would you think is faster? Logic would say that the truncation function should be faster, but unfortunately, it returns a variant, so an extra conversion must take place and a larger chunk of data must be passed. "Integer Dividing" by 1 is actually faster for this task, which really says a lot



Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
Final rant (hopefully ):

Get your program so you can run it without using any of VBs error checking for routine tasks. This way, you can use all the optimisations (overflow checks and array bounds checks) and get your code running at a decent speed.

Rather than telling VB to add in extra checks where they aren''t necessary, explicitly put in checks where they are necessary and then tell VB not to bother. And yes, it does make a big difference, I wrote a program which bubble sorted an array of lots (10 000 from memory) of longs which were in reverse order. That is all it did. Without optimisations, the thing took about 30 seconds (i.e. abysmal), with optimisations, I think it took about 2.

Normally I don''t go on about speed when it isn''t totally related to the question, but since you are talking hash tables, then you are looking for speed and it would be stupid to cancel out all the speed gains by using poor coding.


Trying is the first step towards failure.

Share this post


Link to post
Share on other sites