dcsimg
Results 1 to 7 of 7

Thread: Why am I getting out of range for array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Why am I getting out of range for array?

    Private Sub ButtonDecrpyt_Click(sender As Object, e As EventArgs) Handles ButtonDecrpyt.Click
    MessageBox.Show("encrypted text is : " & Encryptedtext)
    MessageBox.Show("cipheralphabet is : " & CipherAlphabet)

    ' load Cipher alphabet array from cipher alphabet text
    Dim arrCipher() As Char = CipherAlphabet.ToCharArray


    'load encrypted text into array arrEncryptedText
    Dim arrEncryptedText() As Char = Encryptedtext.ToCharArray

    'read encryptedtext Array and convert to plain text
    For i = 0 To arrEncryptedText.Length - 1
    Dim letter As String = arrEncryptedText(i) ' get letter 'i' from encrypted text
    Dim arraypos As Integer = Array.IndexOf(arrCipher, letter) ' find out what position in encrypted text array 'letter' is in
    PlainText &= arrAlpha(arraypos)
    Next
    TextboxPlain.Text = PlainText


    End Sub
    Just to clarify the Message boxes were put in to confirm that both the textstrings 'CipherAlphabet' and 'encryptedText' contain what I expected and they do

    arrAlpha was previously defined under Public Class Form1
    Dim arrAlpha() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

    Been at this now for well over 4 hours trying to work out why I get an out of range exception for line. Debug tells me arraypos is set to -1
    PlainText &= arrAlpha(arraypos)

    I hard-coded "T" instead of letter in the above code line as a test and still get the error, even though I know "T" is the first letter in encrypted text (or index 0 of arrCipher) and should be index value 19 (T being 20th letter of the alphabet) of arrAlpha

    So instead of
    PlainText &= arrAlpha(19)
    VB.NET interprets it as
    PlainText &= arrAlpha(-1)


    I can only think that somehow this line is causing the problem. -1 I believe is the value kicked out if an Item cannot be found in an array.

    Dim arraypos As Integer = Array.IndexOf(arrCipher, letter)

    What I want it to do is look up the VALUE letter in ArrCipher and return the INDEX
    I then use this index value to push out the letter stored in array ArrCipher at the same INDEX

    Hope that makes sense

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Why am I getting out of range for array?

    Try using either all Char arrays and single characters or all string arrays and single characters

    while this would work...

    Code:
    Array.IndexOf(arrCipher, cchar(letter))
    this would be better...

    Code:
    Private Sub ButtonDecrpyt_Click(sender As Object, e As EventArgs) Handles ButtonDecrpyt.Click
        MessageBox.Show("encrypted text is : " & Encryptedtext)
        MessageBox.Show("cipheralphabet is : " & CipherAlphabet)
    
        ' load Cipher alphabet array from cipher alphabet text
        Dim arrCipher() As Char = CipherAlphabet.ToCharArray
    
    
        'load encrypted text into array arrEncryptedText
        Dim arrEncryptedText() As Char = Encryptedtext.ToCharArray
    
        'read encryptedtext Array and convert to plain text
        For i = 0 To arrEncryptedText.Length - 1
            Dim letter As Char = arrEncryptedText(i) ' get letter 'i' from encrypted text
            Dim arraypos As Integer = Array.IndexOf(arrCipher, letter) ' find out what position in encrypted text array 'letter' is in
            PlainText &= CStr(arrAlpha(arraypos))
        Next
        TextboxPlain.Text = PlainText
    
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Why am I getting out of range for array?

    You also need to allow for UpperCase and LowerCase letters which are two distinct versions of a letter and not equal...

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Why am I getting out of range for array?

    Quote Originally Posted by .paul. View Post
    Try using either all Char arrays and single characters or all string arrays and single characters

    while this would work...

    Code:
    Array.IndexOf(arrCipher, cchar(letter))
    this would be better...

    Code:
    Private Sub ButtonDecrpyt_Click(sender As Object, e As EventArgs) Handles ButtonDecrpyt.Click
        MessageBox.Show("encrypted text is : " & Encryptedtext)
        MessageBox.Show("cipheralphabet is : " & CipherAlphabet)
    
        ' load Cipher alphabet array from cipher alphabet text
        Dim arrCipher() As Char = CipherAlphabet.ToCharArray
    
    
        'load encrypted text into array arrEncryptedText
        Dim arrEncryptedText() As Char = Encryptedtext.ToCharArray
    
        'read encryptedtext Array and convert to plain text
        For i = 0 To arrEncryptedText.Length - 1
            Dim letter As Char = arrEncryptedText(i) ' get letter 'i' from encrypted text
            Dim arraypos As Integer = Array.IndexOf(arrCipher, letter) ' find out what position in encrypted text array 'letter' is in
            PlainText &= CStr(arrAlpha(arraypos))
        Next
        TextboxPlain.Text = PlainText
    
    End Sub

    Thank you so much. I have been tinkering with code all over the place and either missed that or didnt think of it.

    Your help is really appreciated. Only 4 days into my adventures with VB.NET, I am slowly getting to grips with it, but seeing mistakes or using code efficiently is going to take a bit of time.
    Thanks for the hint about using all Char arrays and single characters or all string arrays and single characters. I will bare that in mind for the future

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Why am I getting out of range for array?

    Quote Originally Posted by .paul. View Post
    You also need to allow for UpperCase and LowerCase letters which are two distinct versions of a letter and not equal...

    I had thought of that. I just altered the properties of the textbox so that only UPPERCASE could be entered

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,585

    Re: Why am I getting out of range for array?

    not sure if anyone mentioned it but the reason you got a -1 is that the character wasn't found. -1 is an error result.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Why am I getting out of range for array?

    Quote Originally Posted by Lord Orwell View Post
    not sure if anyone mentioned it but the reason you got a -1 is that the character wasn't found. -1 is an error result.
    You're right. I concentrated on fixing the problem and didn't explain the error. An index of -1 is returned if no match is found. Using -1 as an array index causes an out of bounds error because the array lowerbound is 0

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width