Results 1 to 23 of 23

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

  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

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

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

    That code is basically saying Convert an empty string to an Integer, Cint can't do that, if you use TryParse instead and string is empty then the value becomes zero and no more erorrs.

    Instead of an Integer array a List(Of Integer) is much easier to use, just a simple example....

    Code:
    Public Class Form1
    
        Dim MyArray As New List(Of Integer)
    
        Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
            Dim number As Integer
            Integer.TryParse(txtNumbers.Text, number)
            If number <1 Then
                MessageBox.Show("Please enter a number greater then zero.")
            Else
                MyArray.Add(number)
            End If
        End Sub
    
        Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For i As Integer = 0 To MyArray.Count - 1
                txtDisplayNumbers.Text &= MyArray(i).ToString & Environment.NewLine
            Next
        End Sub
    
    End Class
    If you aren't going to do any math with the values you could just use a List (Of String).
    Last edited by Edgemeal; Dec 12th, 2011 at 06:11 PM.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

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

    I totally agree. When you use Redim on an array, with or without the Preserve, you aren't really re-sizing the array. You are actually creating a new array of the right size, then copying the existing into the new. As you might expect, that isn't terribly efficient, though there are some means to accomplish that which make it faster than you might do on your own. If you use a List, you have the Remove and RemoveAt methods, which do exactly what they say, but the List makes much more efficient use of memory for dynamically adding and removing elements.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    Ok thanks you guys for the help but now the rest of my program wont work :/
    I get the following Error : ArguementOutOfRangerExceptin was unhandled. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
    Code:
     Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
    
            Dim Largest As Integer
            Largest = MyArray(0)
            For J = 1 To 24
                If MyArray(J) > Largest Then
                    Largest = MyArray(J)
                End If
            Next
            txtDisplayNumbers.Text = CStr(Largest)
    
        End Sub
    
        Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
    
            Dim Smallest As Integer
            Smallest = MyArray(0)
            For J = 1 To 24
                If MyArray(J) < Smallest Then
                    Smallest = MyArray(J)
                End If
            Next
            txtDisplayNumbers.Text = CStr(Smallest)
    
        End Sub
        Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
    
            Dim Smallest As Integer
            Dim Largest As Integer
            Smallest = MyArray(0)
            Largest = MyArray(0)
            For J = 1 To 24
                If MyArray(J) < Smallest Then
                    Smallest = MyArray(J)
                ElseIf MyArray(J) > Largest Then
                    Largest = MyArray(J)
                End If
            Next
            txtDisplayNumbers.Text = _
            CStr(Smallest) & " to " & CStr(Largest)
    
        End Sub
        Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
    
            Dim Sum As Integer
            For J = 0 To 24
                Sum = Sum + MyArray(J)
            Next
            txtDisplayNumbers.Text = CStr(Sum)
    
        End Sub
    
    
        Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
            Dim lCounter As Long
            Dim dTotal As Double
            Dim dAverage As Double
    
            dTotal = 0
            For lCounter = 0 To UBound(Array)
                dTotal = dTotal + MyArray(CInt(lCounter))
            Next
            dAverage = dTotal / (UBound(Array) + 1)
            txtDisplayNumbers.Text = CStr(dAverage)
        End Sub
    End Class
    Last edited by Hack; Dec 13th, 2011 at 07:29 AM. Reason: Added Code Tags

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    I get the same error where I've highlighted red.

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

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

    Check the value of the upper bound of MyArray before the lines where you get the error.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    Quote Originally Posted by Edgemeal View Post
    That code is basically saying Convert an empty string to an Integer, Cint can't do that, if you use TryParse instead and string is empty then the value becomes zero and no more erorrs.

    Instead of an Integer array a List(Of Integer) is much easier to use, just a simple example....

    Code:
    Public Class Form1
    
        Dim MyArray As New List(Of Integer)
    
        Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
            Dim number As Integer
            Integer.TryParse(txtNumbers.Text, number)
            If number = 0 Then
                MessageBox.Show("Please enter a number greater then zero.")
            Else
                MyArray.Add(number)
            End If
        End Sub
    
        Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For i As Integer = 0 To MyArray.Count - 1
                txtDisplayNumbers.Text &= MyArray(i).ToString & Environment.NewLine
            Next
        End Sub
    
    End Class
    If you aren't going to do any math with the values you could just use a List (Of String).
    Im marking this as resolved if you know how to fix this error can you please private message me, THANK YOU

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Angry Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    I get the following Error everywhere i have highlighted red :

    ArguementOutOfRangerExceptin was unhandled. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
    Code:
    Public Class Form1
    
        Dim Array(24) As Integer
        Dim J As Integer = 0
        Dim MyArray As New List(Of Integer)
    
        Private Sub btnPutNumberIntoMyArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
            Dim number As Integer
            Integer.TryParse(txtNumbers.Text, number)
            MyArray.Add(number)
    
            txtNumbers.Clear()
            txtNumbers.Focus()
    
        End Sub
    
        Private Sub btnDisplayNumbersInMyArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For J As Integer = 0 To MyArray.Count - 1
                txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine
            Next
    
        End Sub
    
    Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
    
    Dim Largest As Integer
    Largest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) > Largest Then
    Largest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Largest)
    
    End Sub
    
    Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
    
    Dim Smallest As Integer
    Smallest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) < Smallest Then
    Smallest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Smallest)
    
    End Sub
    
    Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
    
    Dim Smallest As Integer
    Dim Largest As Integer
    Smallest = MyArray(0)
    Largest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) < Smallest Then
    Smallest = MyArray(J)
    ElseIf MyArray(J) > Largest Then
    Largest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = _
    CStr(Smallest) & " to " & CStr(Largest)
    
    End Sub
    
    Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
    
    Dim Sum As Integer
    For J = 0 To 24
    Sum = Sum + MyArray(J)
    Next
    txtDisplayNumbers.Text = CStr(Sum)
    
    End Sub
    
    Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
    Dim lCounter As Long
    Dim dTotal As Double
    Dim dAverage As Double
    
    dTotal = 0
    For lCounter = 0 To UBound(Array)
    dTotal = dTotal + MyArray(CInt(lCounter))
    Next
    dAverage = dTotal / (UBound(Array) + 1)
    txtDisplayNumbers.Text = CStr(dAverage)
    End Sub
    End Class
    Last edited by Hack; Dec 13th, 2011 at 07:25 AM. Reason: Added Code Tags

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    Quote Originally Posted by vbfbryce View Post
    Check the value of the upper bound of MyArray before the lines where you get the error.
    i'm confused, how would i check the value of the upper bound?
    i don't think i assigned any value to the upper bound

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

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

    If you are using a List, you don't check the upper bound, you check the .Count property. You can't access an item greater than .Count - 1.

    With an array, you always set the upper bound, though that is something of a legacy term. In VB6, you could set the lower and upper bounds, but in .NET all arrays start at 0, so the upper bound is effectively the array size.

    What you were doing was this:
    Code:
    Largest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) > Largest Then
    which will only work if MyArray has at least 25 items in it, because you will access MyArray(24), which is the 25th item in the array.
    My usual boring signature: Nothing

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    Please don't double post. I answered your question in the other thread.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    Code:
    Public Class Form1
    
    Dim Array(24) As Integer
    Dim J As Integer = 0
    Dim MyArray As New List(Of Integer)
    
    
    Private Sub btnPutNumberIntoMyArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
    Dim number As Integer
    Integer.TryParse(txtNumbers.Text, number)
    MyArray.Add(number)
    
    txtNumbers.Clear()
    txtNumbers.Focus()
    
    End Sub
    
    Private Sub btnDisplayNumbersInMyArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
    txtDisplayNumbers.Clear()
    txtDisplayNumbers.Focus()
    
    For J As Integer = 0 To MyArray.Count - 1
    txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine
    Next
    
    End Sub
    
    
    
    Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
    
    Dim Largest As Integer
    Largest = MyArray(0)
    For J = 1 To 24
    [B]If MyArray(J) > Largest Then[/B]
    Largest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Largest)
    
    End Sub
    
    Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
    
    Dim Smallest As Integer
    Smallest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) < Smallest Then
    Smallest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Smallest)
    
    End Sub
    Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
    
    Dim Smallest As Integer
    Dim Largest As Integer
    Smallest = MyArray(0)
    Largest = MyArray(0)
    For J = 1 To 24
    If MyArray(J) < Smallest Then
    Smallest = MyArray(J)
    ElseIf MyArray(J) > Largest Then
    Largest = MyArray(J)
    End If
    Next
    txtDisplayNumbers.Text = _
    CStr(Smallest) & " to " & CStr(Largest)
    
    End Sub
    Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
    
    Dim Sum As Integer
    For J = 0 To 24
    Sum = Sum + MyArray(J)
    Next
    txtDisplayNumbers.Text = CStr(Sum)
    
    End Sub
    
    
    Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
    Dim lCounter As Long
    Dim dTotal As Double
    Dim dAverage As Double
    
    dTotal = 0
    For lCounter = 0 To UBound(Array)
    dTotal = dTotal + MyArray(CInt(lCounter))
    Next
    dAverage = dTotal / (UBound(Array) + 1)
    txtDisplayNumbers.Text = CStr(dAverage)
    End Sub
    End Class
    This is my full code, could you please tell me what i am doing wrong?
    Last edited by Hack; Dec 13th, 2011 at 07:30 AM. Reason: Added Code Tags

  13. #13
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

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

    replace this bit:

    vb Code:
    1. 'DISPLAYS THE CONTENTS OF ARRAY.
    2.  For I = 0 To J - 1
    3.  'Accumulates values in Textbox2 and displays these values.
    4.  txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(mintArray(I)) & ControlChars.NewLine
    5.  Next
    6.  txtDisplayNumbers.Clear()
    7.  txtDisplayNumbers.Focus()
    8.  
    9.  For J = 0 To 24
    10.  txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(Array(J)) & ControlChars.NewLine
    11.  Next
    12.  End Sub

    with:

    vb Code:
    1. txtDisplayNumbers.text = join((from xItem in mintArray where xItem <> 0 select xItem.tostring).toarray, Environment.NewLine)

    Kris

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    Two different questions, my last question was marked resolved.

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

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

    I get this error ;Range variable name cannot match the name of a member of the 'object' class.



    Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
    Static J As Integer
    ReDim Preserve Array(J)

    txtNumbers.Text = CStr(Array(J))
    J = J + 1
    txtDisplayNumbers.Clear()
    txtDisplayNumbers.Text = Join((From xItem In Array Where xItem <> 0 Select xItem.ToString).ToArray, Environment.NewLine)


    End Sub
    Last edited by rosleenXOXO; Dec 12th, 2011 at 06:48 PM.

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    Could you just tell me how i could insert Code2 into code1 correctly?
    Code1:

    Code:
    Option Strict On
    
    Public Class Form1
    
    Dim Array(4) As Integer
    Dim J As Integer = 0
    
    
    
    Private Sub btnPutNumberIntoMyArray_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
    txtDisplayNumbers.Clear()
    txtDisplayNumbers.Focus()
    
    For J As Integer = 0 To Array.Count - 1
    txtDisplayNumbers.Text &= Array(J).ToString & Environment.NewLine
    Next
    
    End Sub
    Private Sub btnDisplayLargestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLargestNumber.Click
    
    Dim Largest As Integer
    Largest = Array(0)
    For J = 1 To 4
    If Array(J) > Largest Then
    Largest = Array(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Largest)
    
    End Sub
    
    Private Sub btnDisplaySmallestNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySmallestNumber.Click
    
    Dim Smallest As Integer
    Smallest = Array(0)
    For J = 1 To 4
    If Array(J) < Smallest Then
    Smallest = Array(J)
    End If
    Next
    txtDisplayNumbers.Text = CStr(Smallest)
    
    End Sub
    Private Sub btnDisplayRange_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
    
    Dim Smallest As Integer
    Dim Largest As Integer
    Smallest = Array(0)
    Largest = Array(0)
    For J = 1 To 4
    If Array(J) < Smallest Then
    Smallest = Array(J)
    ElseIf Array(J) > Largest Then
    Largest = Array(J)
    End If
    Next
    txtDisplayNumbers.Text = _
    CStr(Smallest) & " to " & CStr(Largest)
    
    End Sub
    Private Sub btnDisplaySumOfNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplaySumOfNumbers.Click
    
    Dim Sum As Integer
    For J = 0 To 4
    Sum = Sum + Array(J)
    Next
    txtDisplayNumbers.Text = CStr(Sum)
    
    End Sub
    
    
    Private Sub btnDisplayMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMean.Click
    Dim lCounter As Long
    Dim dTotal As Double
    Dim dAverage As Double
    
    dTotal = 0
    For lCounter = 0 To UBound(Array)
    dTotal = dTotal + Array(CInt(lCounter))
    Next
    dAverage = dTotal / (UBound(Array) + 1)
    txtDisplayNumbers.Text = CStr(dAverage)
    End Sub
    
    Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click
    Dim Key As Integer
    Dim Low As Integer
    Dim High As Integer
    Dim Mid As Integer
    
    
    
    
    Key = CInt(txtBinaryKey.Text)
    Low = 0
    High = 4
    
    While Low <= High
    Mid = (High + Low) \ 2
    If Key = Array(Mid) Then
    lblBinarySearchLocation.BackColor = Color.Silver
    lblBinarySearch.Text = "Search Key found"
    lblBinarySearchLocation.Text = "Array(" & Str(Mid) & ")"
    
    Low = High + 1
    
    ElseIf Key < Array(Mid) Then
    High = Mid - 1
    ElseIf Key > Array(Mid) Then
    Low = Mid + 1
    End If
    End While
    
    If Key <> Array(Mid) Then
    lblBinarySearch.Text = "Search Key not found."
    lblBinarySearchLocation.BackColor = Color.Red
    lblBinarySearchLocation.Text = ""
    End If
    
    End Sub
    
    
    End Class
    *******CODE2:********
    Code:
    ‘TOPIC: USEFUL CODE FOR CONTROLLING UNNECESSARY ZEROS IN ARRAY.
    ‘THERE ARE SEVERAL WAYS TO CONTROL UNNECESSARY ZEROS. THIS IS JUST ONE.
    Public Class Form1
    
    Dim mintArray() As Integer 'Declares Array without giving it a size
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
    Handles Button1.Click
    Static J As Integer
    ReDim Preserve mintArray(J)
    'Gives Array a size and preserves the array values
    mintArray(J) = CInt(TextBox1.Text)
    J=J+1
    TextBox2.Clear()
    
    'DISPLAYS THE CONTENTS OF ARRAY.
    For I = 0 To J - 1
    'Accumulates values in Textbox2 and displays these values.
    TextBox2.Text = TextBox2.Text & CStr(mintArray(I)) & ControlChars.NewLine
    Next
    TextBox1.Clear()
    TextBox1.Focus()
    End Sub
    basically how would would i add code2 into code1 one so i can get rid of the unnecessary zeros produced from not entering 25 elements, i would really appreciate it if you could hep me this.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    I thought we covered that when we said: Use a List rather than an array. Perhaps it wasn't you, but using an array with Redim Preserve is a pretty ineffiicient solution. The List (of T) was introduced, in part, to provide a vastly superior dynamically sized collection.
    My usual boring signature: Nothing

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

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

    If this is for basic class work, and you turn in a LINQ statement, that probably won't go over so well.

    The problem with your loops is that you have made an array with 24 elements. Arrays all start at 0, so the elements are 0-23, not 1-24. You then loop from 1 to 24, which means that the loop will ignore array element 0, which is the first element, and will try to access array element 24, which is the 25th element, and which doesn't exist. Loop from 0 to 23 and all will be well.
    My usual boring signature: Nothing

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    This is an assignment, I HAVE to do it the way my professor has asked and i must use arrays...

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    Classes can sure suck some times.

    So, how do you mean to insert code 2 into code 1? Both of them appear to be a series of methods. In the first snippet, there would be no reason to remove empties from the sum (they make no difference), nor do you really need to remove them from the mean (you'd just leave them out when counting the number of elements). Do you need to remove them for the binary search? That doesn't seem necessary, since the array would be sorted, and it wouldn't really matter whether there were some 0s in the array.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    so basically the professor has given us the code2 which we are supposed to implement into the code1 (which the students have to write). code2 is supposed to remove the unnecessary 0's in the array. Im guessing that code2 has to be written into :
    Code:
    Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
    txtDisplayNumbers.Clear()
    txtDisplayNumbers.Focus()
    
    For J As Integer = 0 To Array.Count - 1
    txtDisplayNumbers.Text &= Array(J).ToString & Environment.NewLine
    Next
    
    End Sub
    Because this is the only button that shows the number the user enters i just cant figure out how to make it work all together...my whole code works except for the extra zeros...

  22. #22

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    there cant be any 0's in the Array, they numbers entered must be larger or equal to 1....and say that i only enter 12 numbers and then the 13(25 in total) other elements pop-up as zeros when i run the method above...

  23. #23
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help with arrays, ArguementOutOfRangeExceptin was unhandled.

    Quote Originally Posted by Shaggy Hiker View Post
    Please don't double post. I answered your question in the other thread.
    Actually you answered the question in this thread as both have now been merged.

    One question, one thread please.

    Also, if you are going to post lengthy pieces of code please do everyone a favor
    and use [code]your code goes in here[/code] tags .

    Thanks.

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