First thing's first. Indent the code. Everything inside of Function...End Function
should be indented one tab or four spaces. Same thing with code inside the If...End If
.
Private Function getbyte(s As String, ByVal place As Integer) As String
If place < Len(s) Then
place = place + 1
getbyte = Mid(s, place, 1)
Else
getbyte = ""
End If
End Function
As for a simpler method for getting a byte at a particular place in a string, I think it's a wash for VB6, but there is an alternative. Create a byte array then return the byte at the index you want to retrieve. How this is done is different in VB6 vs. VB.NET.
VB6
The function to return a byte array in VB6 is StrConv().
Private Function GetByte(ByVal str As String, ByVal place As Integer) as String
bytes() = StrConv(str, vbFromUnicode)
GetByte = bytes[place + 1]
End If
I did not implement any checks on the place parameter, but it should check for both place < Len(str)
and place > Len(str)
.
Note that I used ByVal
for both parameters. You had only used it for one of them in the code above. By default, references are passed ByRef
, so it's good practice to declare it so we know that functions won't have side effects and go changing s
on us. Speaking of s
, single letter parameter names are the devil in VB6. The IDE isn't smart enough to replace all instances of it like in VB.NET. Try to do a find and replace on "s" and see what happens. Opt for a longer more meaningful name. Even dsmvwlng string
to str
would be preferable over s
.
I think the old VB6 conventions should be dropped in preference of the new VB.NET naming conventions. Methods should be PascalCased. Note that I changed getByte
to GetByte
.
VB.NET
There is a built in string function to do exactly what your function does. It's called GetChar
. MSDN documentation.