Results 1 to 3 of 3

Thread: [RESOLVED] Overlap through array?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Resolved [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".

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Overlap through array?

    You simply add the count, ie:

    NewIndex = (Count + OldIndex - 2) Mod Count

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width