Hello, I am trying to create a currency converter, but when I press 'convert' it get the error message:
Index was outside the vounds of the array.

Here is my code:

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub doCalculate()
        ' Need the scraping
        Dim Str As System.IO.Stream
        Dim srRead As System.IO.StreamReader
        Dim strAmount As String

        strAmount = currencyAmount.Text


        ' Get values from the textboxes
        Dim strFrom() As String = Split(currencyFrom.Text, " - ")
        Dim strTo() As String = Split(currencyTo.Text, " - ")

        Dim xe As Object
        xe = "http://www.xe.com/ucc/convert.cgi?Amount=" & strAmount & "&From=" & strFrom(1) & "&To=" & strTo(1) & "YER&image.x=47&image.y=19&image=Submit"

        ' Web fetching variables
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(xe)
        Dim resp As System.Net.WebResponse = req.GetResponse

        Str = resp.GetResponseStream
        srRead = New System.IO.StreamReader(Str)


        ' Match the response
        Try
            Dim myMatches As MatchCollection
            Dim myRegExp As New Regex("([0-9]+\.[0-9]+ " + strTo(1) + ")")

            myMatches = myRegExp.Matches(srRead.ReadToEnd)

            ' Search for all the words in the string
            Dim sucessfulMatch As Match
            For Each sucessfulMatch In myMatches
                mainText.Text = sucessfulMatch.Value
            Next
        Catch ex As Exception
            mainText.Text = "Unable to connect to XE"
        Finally
            ' Close the streams
            srRead.close()
            Str.Close()
        End Try
        convertToLabel.Text = strAmount + " " + strFrom(0) + " Converts To: "
    End Sub
    Private Sub convertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertButton.Click
        ' Check if the currency is a number
        If Not IsNumeric(currencyAmount.Text) Then
            MsgBox("Please enter a valid amount!", MsgBoxStyle.Information, "Invalid Input")
            currencyAmount.Focus()
            Return
        End If

        doCalculate()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        currencyFrom.selectedIndex = 0
        currencyTo.SelectedIndex = 1
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertbutton.Click

    End Sub
End Class

The line getting the error is line 20
(xe = "http://www.xe.com/ucc/convert.cgi?Amount=" & strAmount & "&From=" & strFrom(1) & "&To=" & strTo(1) & "YER&image.x=47&image.y=19&image=Submit")

Also; in a rich text box, how would I make it so that if a user types "^1" in a RTB, that all the text after the "^1" would be red? and if there is "^2" it would be blue from that point onwards, and if there was "^^B" that it was bold, and kept the color from that point onwards?

Recommended Answers

All 3 Replies

Have you verified that your split statements are returning the amount you think they are?

Place a break point in the application after the split.

Then use intellisence to view the data inside the object.

Like Begginnerdev said check the To and From values Also? why use a textbox would you not be better with a combobox and give the users fixed values of currency?

In the strFrom(1), try to replace the number to 0 since your error is mainly about an array being out of bound. Remember that when it compiles, it starts at 0. I hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.