Help with Array.ConvertAll

twotech

New member
Joined
Jun 6, 2008
Messages
2
Programming Experience
Beginner
Can someone please help me with the Array.ConvertAll. I'm trying to use it so that i can convert a String array to a Integer array and then sort it with the Array.Sort command.

Below is some of the code.

VB.NET:
Public Class frmMain

    Dim str As String
    Dim strSorted As String
    Dim splLen As Integer
    Dim arrStr() As String
    Dim arrInt() As Integer
    Dim converter As Converter(Of String, Integer)


    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        str = txtList.Text
        arrStr = str.Split(" ")
        splLen = arrStr.GetLength(0) - 1
        arrInt = Array.ConvertAll(arrStr, converter)
        Array.Sort(arrInt)
        For index As Integer = splLen To 0 Step -1
            strSorted = strSorted & arrInt(index).ToString() & " "
        Next

        txtSorted.Text = strSorted



    End Sub
End Class

I get a System.ArgumentNullException exception on line

VB.NET:
arrInt = Array.ConvertAll(arrStr, converter)

Help would be greatly appreciated. Hope this is the right section to ask.
 
VB.NET:
    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        Str = txtList.Text
        arrStr = Str.Split(" ")
        splLen = arrStr.GetLength(0) - 1
        'arrInt = Array.ConvertAll(arrStr, converter)
        Dim arrInt() As Integer = Array.ConvertAll(arrStr, New Converter(Of String, Integer)(AddressOf ConvertStringToInt))
        Array.Sort(arrInt)
        For index As Integer = splLen To 0 Step -1
            strSorted = strSorted & arrInt(index).ToString() & " "
        Next

        txtSorted.Text = strSorted
    End Sub

    Public Shared Function ConvertStringToInt(ByVal str As String) As Integer
        Return CInt(str)
    End Function

I was able to get it working like this. If there's a better way hopefully someone else can step in and provide additional information.
 
Thank you, works like a charm. The usage of this method wasn't clear on the MSDN website and since I'm a bit new to VB a wasn't able to figure it out. Just had to clear the strSorted variable everytime the button is clicked and arrInt can be declared outside of the button click subprocedure.
 
...Just had to clear the strSorted variable everytime the button is clicked and arrInt can be declared outside of the button click subprocedure.

If I'm not going to use a variable outside of the sub I prefer to limit its scope to just that procedure.
 
Back
Top