arraylist: getting highest value

mcfly

Well-known member
Joined
Jun 15, 2009
Messages
54
Programming Experience
Beginner
Hi there,

Here I have some code which when called should give me a new event id, the event ids are quite simple C1, C2, C3 etc etc, the database doesn't produce the auto-number part I would like to do this with the code below:

The autonumbers aren't in any partcular order in the database.

So far this code will create an array list of all of the ID's, strip the 'C' part out of the number and sort them but next I need it grab the uppermost one within the array list, is there an easy command to do this?...






VB.NET:
Public Sub get_new_event_id()
        'open calendar information
        'todo move to module call()
        Dim connetionString As String
        Dim oledbCnn As OleDbConnection
        Dim oledbCmd As OleDbCommand
        Dim sql As String

        Dim arrayListInfo As New ArrayList()
        Dim item As String

        'todo set password and user for database
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=H:\My Pictures\hotel_db.mdb;User Id=admin;Password=;"
        sql = "Select * from tbl_calendar"

        oledbCnn = New OleDbConnection(connetionString)

        Try
            'open and prepare connection
            oledbCnn.Open()
            oledbCmd = New OleDbCommand(sql, oledbCnn)
            Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            Do While oledbReader.Read

                'add event id's to an array
                item = oledbReader("cal_date_id")

                'remvoe the first character C from the ID's in the array
                item = item.Remove(0, 1)

                'convert to integer
                item = Integer.Parse(item)

                'add to our array
                arrayListInfo.Add(item)

                arrayListInfo.Sort()

            Loop

            [COLOR="Red"]'TODO grab highest value in array[/COLOR]
            'convert to string then concatenate C + number


            MsgBox("Your number " & item)

            oledbReader.Close()
            oledbCmd.Dispose()
            oledbCnn.Close()
        Catch ex As Exception
            'get error message
            MsgBox(ex.GetType.Name & " : " & ex.Message)
        End Try

    End Sub
End Class
 
item = integer.parse() doesn't make sense to me, because item is a string anyway. This means that you convert a string to an int and then it's automatically converted to string again.

If you need the "highest" item number, why don't you simply add an "order by cal_date_id"?
 
Back
Top