Results 1 to 23 of 23

Thread: Removing unnecessary 0's from an array? " InvalidCastException was unhandled"

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Resolved Removing unnecessary 0's from an array? " InvalidCastException was unhandled"

    I have an array with a possible of 25 numbers that can be entered into it, i need help figuring out how to take out the unnecessary zero's for the valueas i have not entered into my array.
    Code:
    Public Class Form1
       
      Dim mintArray() As Integer 'Declares Array without giving it a size
      Dim Array(24) As Integer
      Dim J As Integer = 0
    
    
    Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
    
            Array(J) = CInt(txtNumbers.Text)
            J = J + 1
            txtNumbers.Clear()
            txtNumbers.Focus()
    
        End Sub
    
    Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            Static J As Integer
            ReDim Preserve mintArray(J)
            'Gives Array a size and preserves the array values
            mintArray(J) = CInt(txtDisplayNumbers.Text)
            J = J + 1
            txtDisplayNumbers.Clear()
    
            'DISPLAYS THE CONTENTS OF ARRAY.
            For I = 0 To J - 1
                'Accumulates values in Textbox2 and displays these values.
                txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(mintArray(I)) & ControlChars.NewLine
            Next
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For J = 0 To 24
                txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(Array(J)) & ControlChars.NewLine
            Next
        End Sub 
    
    End Class
    I get an error at the highlighted part : " InvalidCastException was unhandled, Conversion from string "" to type 'integer' is not valid."
    Last edited by Hack; Dec 13th, 2011 at 07:28 AM. Reason: Added Code Tags

Tags for this Thread

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