Passing a String as Pointer

R

robertjhuff

Hi,

Below is a snippet of code that works compiled in a MDE application:

============= SNIP BEGIN =====================

Public Function LevDist(ByVal pStr1, ByVal pStr2) As Integer
'******************** Levenshtein Distance
**************************************************
'* Levenshtein Distance algorithm is named after the Russian scientist
'* Vladimir Levenshtein, who devised the algorithm in 1965
'*
'* Levenshtein edit distance is the number of insertions, deletions, or
'* replacements of single characters that are required to convert one
'* string to the other.
'*
'* Character transposition is detected in Step 6A.
'* Transposition is given a cost of 1.
'*
'* Normally Levenshtein edit distance is symmetric.
'* That is, LevDist(pStr1 , pStr2) is the same as LevDist(pStr2 ,
pStr1).
'*
'********************************************************************************************
Dim n As Integer, m As Integer, matrix() As Integer
Dim i As Integer, j As Integer, cost As Integer, t_j, s_i
Dim above As Integer, left As Integer, diag As Integer, cell As
Integer
Dim trans As Integer


' Step 1

n = UBound(pStr1)
m = UBound(pStr2)

============= SNIP ENDS=====================

When I copy the code into a Access 2003 Module and attempt to execute
it fails with a mis-match type error upon the statement 'n =
UBound(pStr1)' which seems correct as a string is being passed.

And yet the code works in the MDE!

What am I missing?

Thanks,
Bob
 
M

Malcolm Smith

Bob

UBound(asBeans) returns the number of elements in the array.

So if you declared

dim asBeans(6) as string

then

UBound(asBeans)

would return 6.



If pStr1 is a string then perhaps you want to know the length of the
string, and then you should use the Len() function to determine the length
of the string.

The trouble is pStr1 and pStr2 are typed so they could be anything! And
this is when things could get a little hairy to say the least.

Hope that this helps
Malc
 
K

Karl E. Peterson

Hi Bob --
Public Function LevDist(ByVal pStr1, ByVal pStr2) As Integer

Never use Variants when you don't have to. This is such a case. 32-bit
pointers are, uh, 32-bits. Exactly. Use Long variables to hold them. To
obtain a pointer to a String in VB, use the StrPtr() function. Ex:

Call LevDist(StrPtr(MyStr1), StrPtr(MyStr2))
When I copy the code into a Access 2003 Module and attempt to execute
it fails with a mis-match type error upon the statement 'n =
UBound(pStr1)' which seems correct as a string is being passed.

What is "correct" in this situation? I have absolutely no concept what the
UBound of a pointer may be. VB won't either. I think this algorithm needs
to be re-examined in light of what datatypes you have available to work
with.

Later... Karl
 
J

Jonathan West

Hi Robert,

I suspect that pStr1 and pStr2 are strings. In which case, you need to use
Len rather than UBound to get the length of the strings.

By the way, you have posted to a Word VBA group rather than an Access group.
As it happens, the question is a general VBA one and you have as good a
chance of getting an answer here as anywhere, but if you have more
application-specific questions in future, you might want to make sure you
direct your question to a group that is most likely to have experts who can
answer you.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Doug Robbins - Word MVP

You missed the newsgroup to which you should post this. The one where it
landed is for the use of Visual Basic in Word. I suggest that you post your
question to microsoft.public.access.modulescoding

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top