Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27159

Conversion from string "Dr. No" to type 'Double' is not valid

$
0
0
Hello Everyone,
I am having trouble with this error. When I run the program and try to load the necessary files I get this error: Conversion from string "Dr. No" to type 'Double' is not valid. Here is the codes:
Code:

Option Strict On

Public Class John_Everett_P3
    'array list to hold data from files
    Dim mydata As New ArrayList()
    Dim myprice As New ArrayList()
    'clear everything
    Private Sub ResetClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetClearToolStripMenuItem.Click
        resultTextBox.Text = ""
        top3rdLabel.Text = ""
        statusLabel.Text = "Data Cleared."
        mydata.Clear()
        myprice.Clear()
    End Sub

    'exit app by menu
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Dim confirm As String
        confirm = CStr(MessageBox.Show("Are you sure want to Exit program?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        If CDbl(confirm) = MsgBoxResult.Yes Then
            Application.Exit()
        End If
    End Sub
    'exit app by button
    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Dim confirm As String
        confirm = CStr(MessageBox.Show("Are you sure want to Exit program?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        If CDbl(confirm) = MsgBoxResult.Yes Then
            Application.Exit()
        End If
    End Sub

    'clear app data
    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        resultTextBox.Text = ""
        top3rdLabel.Text = ""
        statusLabel.Text = "Data Cleared."
        mydata.Clear()
        myprice.Clear()
    End Sub
    'load files
    Private Sub loadfilesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadfilesButton.Click
        Dim openfilesDialog As New OpenFileDialog()
        With openfilesDialog
            .CheckPathExists = True
            .DefaultExt = "txt"
            .Filter = _
            "Text files (*.txt)|*.txt"
            .Multiselect = True
            .RestoreDirectory = True
            .ShowHelp = True
            .ShowReadOnly = False
            .ReadOnlyChecked = False
            .Title = "Open"
            .ValidateNames = True
        End With

        'get files
        If openfilesDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim countfiles As Integer = 0
            For Each s As String In openfilesDialog.FileNames
                Dim sAllData() As String = System.IO.File.ReadAllLines(s)
                countfiles = countfiles + 1
            Next
            'if file count is < 2 or > than 2
            If countfiles < 2 Or countfiles > 2 Then
                Dim confirm As String
                confirm = CStr(MessageBox.Show("Please select two files", "P4", MessageBoxButtons.OK))
                Return
            End If

            'Build string with all filenames selected
            For Each s As String In openfilesDialog.FileNames
                Dim sAllData() As String = System.IO.File.ReadAllLines(s)
                For i As Integer = 0 To sAllData.Length - 1
                    Dim strArr() As String
                    strArr = sAllData(i).Split(CChar(vbTab))
                    mydata.Add(strArr(0))
                    myprice.Add(strArr(0))
                Next
            Next
            resultTextBox.Text = ""
            resultTextBox.Text = displayResult(mydata, myprice)
            top3rdLabel.Text = displayTop3rd(mydata, myprice)
            statusLabel.Text = "Files loaded sucessfully"
        End If
    End Sub

    'sort by Title A-Z
    Private Sub sortatozButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sortAZButton.Click
        If (resultTextBox.Text.Count <= 0) Then
            Dim confirm As String
            confirm = CStr(MessageBox.Show("Please first load two files for this opearation", "P4", MessageBoxButtons.OK, MessageBoxIcon.Error))
            Return
        End If

        Dim string_array1() As String
        string_array1 = DirectCast(mydata.ToArray(GetType(String)), String())
        Dim string_array2() As String
        string_array2 = DirectCast(myprice.ToArray(GetType(String)), String())

        mydata.Clear()
        myprice.Clear()

        Dim len As Integer = string_array1.Length - 1
        For i As Integer = 0 To len
            For j As Integer = 0 To len - 1
                If String.Compare(string_array1(j), string_array1(j + 1)) > 0 Then
                    Dim temp As String
                    temp = string_array1(j)
                    string_array1(j) = string_array1(j + 1)
                    string_array1(j + 1) = temp

                    temp = string_array2(j)
                    string_array2(j) = string_array2(j + 1)
                    string_array2(j + 1) = temp

                End If
            Next j
        Next i

        For i As Integer = 0 To len
            mydata.Add(string_array1(i))
            myprice.Add(string_array2(i))
        Next
        resultTextBox.Text = ""
        resultTextBox.Text = displayResult(mydata, myprice)
        statusLabel.Text = "Sorted By Title A-Z"

    End Sub
    'display results
    Function displayResult(ByVal mydata As ArrayList, ByVal myprice As ArrayList) As String
        Dim result As String = "Title                          " & vbTab & "Box Office Amount" & vbCrLf
        result = result & "-------------------------------" & vbTab & "-----------------" & vbCrLf
        For i As Integer = 0 To mydata.Count - 1
            result = result & mydata(i).ToString
            For sp As Integer = 1 To CInt(31 - CDbl(mydata(i).ToString))
                result = result & " "
            Next
            result = result & vbTab & FormatCurrency(Double.Parse(CStr(myprice(i)))) & vbCrLf
        Next
        Return result

    End Function
    'display instructions
    Private Sub InstructionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstructionsToolStripMenuItem.Click
        Dim message As String = ""
        message = "1. Click the 'Load Files button" & vbCrLf & vbCrLf
        message = message & "2. Select two respective files." & vbCrLf
        message = message & "      (Note: Each file must have the same number of lines.)" & vbCrLf
        message = message & "      (Note: You may select only two files)" & vbCrLf & vbCrLf
        message = message & "3: Select the 'Open' button toload selected files." & vbCrLf & vbCrLf
        message = message & "4. The Box Office Lister will now display each file's contents as it appears" & vbCrLf
        message = message & "in the file." & vbCrLf & vbCrLf
        message = message & "5. Clicking any of the sort buttons will reorder the displayed contents as " & vbCrLf
        message = message & "follows:" & vbCrLf
        message = message & "      a) Sort by A - Z: Sorts the displyed contents alphabetically by title" & vbCrLf
        message = message & "      b) Sort by Z - A: Sorts the displyed contents by title in reverse alphabetically order" & vbCrLf
        message = message & "      c) Sort by 0 - 9: Sorts the displyed contents in ascending order by Box Office Amount" & vbCrLf
        message = message & "      d) Sort by 9 - 0: Sorts the displyed contents in descending order by Box Office Amount" & vbCrLf & vbCrLf

        message = message & "6. Clicking(the) 'Clear' button will reset the program and require you to " & vbCrLf
        message = message & "select  two new files" & vbCrLf & vbCrLf

        message = message & "7. Clicking(the) 'Exit' button will close the program" & vbCrLf & vbCrLf

        message = message & "8. TOP THIRD" & vbCrLf
        message = message & "      Top Third lists to top 1/3 mission in order of Box Office Amount"

        Dim ask As String
        ask = CStr(MessageBox.Show(message, "007 Box Office Lister Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information))
        If CDbl(ask) = MsgBoxResult.Ok Then
        End If
    End Sub

    'sort by Title Z-A
    Private Sub sortztoaButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sortztoaButton.Click

        If (resultTextBox.Text.Count <= 0) Then
            Dim confirm As String
            confirm = CStr(MessageBox.Show("Please first load two files for this opearation", "P4", MessageBoxButtons.OK, MessageBoxIcon.Error))
            Return
        End If

        Dim string_array1() As String
        string_array1 = DirectCast(mydata.ToArray(GetType(String)), String())
        Dim string_array2() As String
        string_array2 = DirectCast(myprice.ToArray(GetType(String)), String())

        mydata.Clear()
        myprice.Clear()

        Dim len As Integer = string_array1.Length - 1
        For i As Integer = 0 To len
            For j As Integer = 0 To len - 1
                If String.Compare(string_array1(j), string_array1(j + 1)) < 0 Then
                    Dim temp As String
                    temp = string_array1(j)
                    string_array1(j) = string_array1(j + 1)
                    string_array1(j + 1) = temp

                    temp = string_array2(j)
                    string_array2(j) = string_array2(j + 1)
                    string_array2(j + 1) = temp

                End If
            Next j

        Next i

        For i As Integer = 0 To len
            mydata.Add(string_array1(i))
            myprice.Add(string_array2(i))
        Next

        resultTextBox.Text = ""
        resultTextBox.Text = displayResult(mydata, myprice)
        statusLabel.Text = "Sorted By Title Z-A"
    End Sub
    'sort by Box Office 0-9
    Private Sub sort0to9Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sort0to9Button.Click

        If (resultTextBox.Text.Count <= 0) Then
            Dim confirm As String
            confirm = CStr(MessageBox.Show("Please first load two files for this opearation", "P4", MessageBoxButtons.OK, MessageBoxIcon.Error))
            Return
        End If

        Dim string_array1() As String
        string_array1 = DirectCast(mydata.ToArray(GetType(String)), String())

        Dim string_array2() As String
        string_array2 = DirectCast(myprice.ToArray(GetType(String)), String())

        mydata.Clear()
        myprice.Clear()

        Dim len As Integer = string_array1.Length - 1
        For i As Integer = 0 To len

            For j As Integer = 0 To len - 1
                If String.Compare(string_array2(j), string_array2(j + 1)) > 0 Then
                    Dim temp As String

                    temp = string_array2(j)
                    string_array2(j) = string_array2(j + 1)
                    string_array2(j + 1) = temp

                    temp = string_array1(j)
                    string_array1(j) = string_array1(j + 1)
                    string_array1(j + 1) = temp

                End If
            Next j
        Next i

        For i As Integer = 0 To len
            mydata.Add(string_array1(i))
            myprice.Add(string_array2(i))
        Next

        resultTextBox.Text = ""
        resultTextBox.Text = displayResult(mydata, myprice)
        statusLabel.Text = "Sorted By Box Office 0-9"
    End Sub
    'sort by Box Office 9-0
    Private Sub sort9to0Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sort9to0Button.Click

        If (resultTextBox.Text.Count <= 0) Then
            Dim confirm As String
            confirm = CStr(MessageBox.Show("Please first load two files for this opearation", "P4", MessageBoxButtons.OK, MessageBoxIcon.Error))
            Return
        End If

        Dim string_array1() As String
        string_array1 = DirectCast(mydata.ToArray(GetType(String)), String())

        Dim string_array2() As String
        string_array2 = DirectCast(myprice.ToArray(GetType(String)), String())

        mydata.Clear()
        myprice.Clear()

        Dim len As Integer = string_array1.Length - 1
        For i As Integer = 0 To len

            For j As Integer = 0 To len - 1
                If String.Compare(string_array2(j), string_array2(j + 1)) < 0 Then
                    Dim temp As String

                    temp = string_array2(j)
                    string_array2(j) = string_array2(j + 1)
                    string_array2(j + 1) = temp

                    temp = string_array1(j)
                    string_array1(j) = string_array1(j + 1)
                    string_array1(j + 1) = temp

                End If
            Next j

        Next i

        For i As Integer = 0 To len
            mydata.Add(string_array1(i))
            myprice.Add(string_array2(i))
        Next

        resultTextBox.Text = ""
        resultTextBox.Text = displayResult(mydata, myprice)
        statusLabel.Text = "Sorted By Box Office 9-0"
    End Sub

    'display top 1/3 list elements
    Function displayTop3rd(ByVal mydata As ArrayList, ByVal myprice As ArrayList) As String

        Dim string_array1() As String
        string_array1 = DirectCast(mydata.ToArray(GetType(String)), String())

        Dim string_array2() As String
        string_array2 = DirectCast(myprice.ToArray(GetType(String)), String())


        Dim len As Integer = string_array1.Length - 1
        For i As Integer = 0 To len
            For j As Integer = 0 To len - 1
                If String.Compare(string_array2(j), string_array2(j + 1)) < 0 Then
                    Dim temp As String

                    temp = string_array2(j)
                    string_array2(j) = string_array2(j + 1)
                    string_array2(j + 1) = temp

                    temp = string_array1(j)
                    string_array1(j) = string_array1(j + 1)
                    string_array1(j + 1) = temp

                End If
            Next j
        Next

        Dim result As String = ""
        For i As Integer = 0 To CInt(string_array1.Length / 3)

            result = result & (i + 1).ToString() & ". " + string_array1(i) & vbCrLf

        Next
        Return result

    End Function

    Private Sub John_Everett_P4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

Here is the line where the error occurs:
Code:

For sp As Integer = 1 To CInt(31 - CDbl(mydata(i).ToString))
Can anyone help me resolve this?

Viewing all articles
Browse latest Browse all 27159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>