CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Nov 2007
    Posts
    1

    How to fill a List(Of )

    Hope someone can help me with this I just can't get my head around it.
    I got this code off the net:
    ---------------------------------------------------------------------------------------------
    Code:
      Public Class Book
            Private m_Name As String
            Private m_Chapters As List(Of Chapter)
    
            Public Property Name() As String
                Get
                    Return m_Name
                End Get
                Set(ByVal value As String)
                    m_Name = value
                End Set
            End Property
    
            Public Property Chapters() As List(Of Chapter)
                Get
                    Return m_Chapters
                End Get
                Set(ByVal value As List(Of Chapter))
                    m_Chapters = value
                End Set
            End Property
    
        End Class
    
    
        Public Class Chapter
            Private m_Title As String
            Private m_Page As String
    
            Public Property Title() As String
                Get
                    Return m_Title
                End Get
                Set(ByVal value As String)
                    m_Title = value
                End Set
            End Property
    
            Public Property Page() As String
                Get
                    Return m_Page
                End Get
                Set(ByVal value As String)
                    m_Page = value
                End Set
            End Property
    
        End Class
    
        Public Sub New()
            InitializeComponent()
            Dim books As New ObservableCollection(Of Book)()
    
            For i As Integer = 0 To 4
                Dim book As New Book() With {.Name = "Book" + i.ToString()}
    
                book.Chapters = New List(Of Chapter)()
    
                For j As Integer = 0 To 4
                    book.Chapters.Add(New Chapter() With {.Title = "Chapter" + i.ToString() + "_" + j.ToString()})
                Next
    
                books.Add(book)
            Next
    
            Me.MainList.ItemsSource = books
        End Sub
    ----------------------------------------------------------------------------------------------------
    As you can see there is a List(Of Chapter)
    And in the code it is populated with Hard Codeing "Chapter" into book.Chapters.Add
    I need to know how do you do this with data from a SQL Database table.
    So if I had two tables in a database one called Books and the other called Chapters
    Books

    BookID1 Book1

    BookID2 Book2



    Chapters

    ChapterID1 BookID1 Chapter1

    ChapterID2 BookID1 Chapter2

    ChapterID3 BookID2 Chapter1a

    ChapterID4 BookID2 Chapter2a

    ChapterID5 BookID2 Chapter3a

    How do I get this data into the List(of )?
    Please help
    thank you
    Last edited by Cimperiali; February 27th, 2012 at 12:33 PM. Reason: added [code][/code] tags

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

Featured