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
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)
getbyte = ""
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.
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]
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
str would be preferable over
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
There is a built in string function to do exactly what your function does. It's called
GetChar. MSDN documentation.