-
Aug 24th, 2019, 03:36 PM
#1
Thread Starter
New Member
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
-
Aug 24th, 2019, 04:03 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 24th, 2019, 04:05 PM
#3
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 24th, 2019, 08:09 PM
#4
Thread Starter
New Member
Re: Why am I getting out of range for array?
Originally Posted by .paul.
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
-
Aug 24th, 2019, 08:14 PM
#5
Thread Starter
New Member
Re: Why am I getting out of range for array?
Originally Posted by .paul.
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
-
Aug 25th, 2019, 10:03 PM
#6
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.
-
Aug 26th, 2019, 12:00 AM
#7
Re: Why am I getting out of range for array?
Originally Posted by Lord Orwell
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|