[RESOLVED] Overlap through array?
Hi.
I have a string array with the contents of the alphabet:
MyString(0) = "a"
MyString(1) = "b"
etc
I have a function that takes a letter and an integer and returns the letter.
For example:
GetAlpha("c",2) would return "e" because 2 placements ahead of c is e.
For the function above if I get a number that exceeds the upper bound I use the mod operator to 'overlap?' again. This works fine from what I have tested.
But the problem is if I use a negative number that exceeds the lower bound, how do I overlap from the end?
Like if I do GetAlpha("a",-2) if would return "y".
Re: Overlap through array?
You simply add the count, ie:
NewIndex = (Count + OldIndex - 2) Mod Count
Re: Overlap through array?
In the other hand: why have an array?
Code:
Option Explicit
Public Function GetAlpha(ByRef Character As String, ByVal Jump As Integer) As String
Dim lngChar As Long
' get the character code
lngChar = AscW(Character) - 97
' must be a - z
If lngChar >= 0 And lngChar <= 25 Then
' validate jump
If Jump >= 0 Then
Jump = Jump Mod 26
Else
Jump = -(-Jump Mod 26)
End If
' now generate the character
GetAlpha = ChrW$((26 + lngChar + Jump) Mod 26 + 97)
End If
End Function
Private Sub Form_Load()
Debug.Print GetAlpha("a", 28)
Debug.Print GetAlpha("a", -28)
End Sub