Results 1 to 9 of 9

Thread: Add Item To ArrayList

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    5

    Unhappy Add Item To ArrayList

    Hi, I Have some problem to add ListBox Item to ArrayList.
    can anyone help me what I do wrong?

    Code:
     Dim myStreamReader As StreamReader = File.OpenText(LastFile) 'Last Opened File 
    
            Dim intListBoxTabs() As Integer = {80, 180} 
            Dim intRes As Integer 
            Dim myInputString As String 
            Dim t As Task 
            Dim Prio As Integer 
            Dim Due As Date 
            Dim ToDo As String 
    
            List.Items.Clear() 
            intRes = SendMessage(List.Handle, _ 
                                   LB_SETTABSTOPS, _ 
                                   intListBoxTabs.Length, _ 
                                   intListBoxTabs(0)) 
    
            While myStreamReader.Peek() > -1 
                myInputString = myStreamReader.ReadLine() 
    
    
                If Not myInputString Is Nothing Then 
    
                      t = New Task(Prio, Due, ToDo)  
                    tasks.Add(t) 
    
                End If 
            End While 
    
            Dim i As Integer 
            Dim s As String = "" 
    
            If tasks.Count > 0 Then 
                For i = 0 To tasks.Count - 1 
                    't = Split(myInputString, ",") 
                    List.Items.Add(Prio & vbTab & Due & vbTab & ToDo) 
                Next 
            End If 
            tasks.Sort() 
    
    
            myStreamReader.Close() 
        End Sub 
    
    End Class


    Thanks

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    I'm guessing that tasks is your ArrayList and that the red line is where you're having trouble?

    Are you getting an error message? If so, what is it?

    Can you let us see the definition of your Task class?
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    5
    Hi trisuglow, you're right.

    I'm having trouble there ( or somewhere else)

    Here is the class...

    Code:
     
    Public Class Task
        Implements IComparable
        Private mPrio As Integer
        Private mDue As DateTime
        Private mToDo As String
    
        Public Sub New(ByVal Prio As Integer, _
                        ByVal Due As DateTime, _
                        ByVal ToDo As String)
            mPrio = Prio
            mDue = Due
            mToDo = ToDo
        End Sub
    
        Public Function Prio() As Integer
            Return mPrio
        End Function
    
        Public Function Due() As DateTime
            Return mDue
        End Function
    
        Public Overloads Function CompareTo(ByVal t1 As Object) As Integer Implements IComparable.CompareTo
            If t1.Prio() > mPrio Then
                Return 1
            ElseIf t1.Prio < mPrio Then
                Return -1
            Else
                'the two tasks have equal priority
                Return DateTime.Compare(mDue, t1.due())
            End If
        End Function
    
        Public Overrides Function ToString() As String
            Return mPrio.ToString & "_" & mDue.ToShortDateString() & "_" & mToDo
        End Function
    End Class

    Thanks

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Just a thought...

    You are declaring t as a New ArrayList aren't you?


    Tris.
    This world is not my home. I'm just passing through.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    5
    Yes, you're right

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by trisuglow


    You are declaring t as a New ArrayList aren't you?


    And that's the problem Marcels... you are making a new Arraylist everytime you want to add something instead of adding it to the same one.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    5
    Do you have another idea, if I want to add everything into the same one?

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Remove t = New Task(Prio, Due, ToDo) from the if block.

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by marcels
    Do you have another idea, if I want to add everything into the same one?
    1) You haven't included the code where you declare tasks. I see a line tasks.add(t), but I don't see the decleration. Where is it?

    2) Also, you haven't said what the error is, or what you are trying to do... you should give more information, otherwise this will turn into a 20 post thread and people will view it and think ***.

    3) You only need to declare one instance of your Task class. But you do need a method to ADD things. I don't see one in your class. All I see is a NEW function, which does little to help you list tasks all in one place, much less compare them.

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