Consulting

Results 1 to 3 of 3

Thread: Nested classes with dynamic array

  1. #1

    Nested classes with dynamic array

    Hi,
    How can i define a dynamic array of a class object in another class?
    Beneath you can find a demo code, what did I forget / wrong?

    Main:


    Sub
    Dim library as Collection
    Dim book as clsBook
    Set library = New Collection

    'add books to library
    For i=0 to 5
    Set book = New clsBook
    book.title = "title"

    'add pages to book
    For j = 0 to 5
    book.page(j).pagenr = "P" & j
    Next j

    library.Add book
    Next i
    End Sub

    clsBook:
    Dim title as String
    Dim pages() as clsPage '??? How to create a dynamic array of the class clsPage


    clsPage:
    Dim pagenr as Integer

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I'd do it a little different - this might give you some ideas


    Main sub in standard module

    Option Explicit
    
    
    
    
    Sub Main()
        Dim i As Long, j As Long
        Dim BookLibrary As Collection
        Dim oBook As clsBook
        Dim oPage As clsPage
        Dim s As String
        
        Set BookLibrary = New Collection
    
    
        'add books to BookLibrary
        For i = 1 To 5      '   Collection Items start at 1
            Set oBook = New clsBook
            oBook.BookTitle = "Book Title " & i
    
    
            'add pages to book
            For j = 1 To 5
                Set oPage = New clsPage
                oPage.PageNumber = j
                oPage.PageText = "This is text for " & oBook.BookTitle & " Page " & CStr(j)
                Call oBook.BookPages.Add(oPage, CStr(j))
            Next j
                
            BookLibrary.Add oBook, oBook.BookTitle
        
        
        Next i
    
    
        MsgBox BookLibrary(1).BookTitle
        MsgBox BookLibrary(5).BookTitle
        MsgBox BookLibrary(3).BookPages(1).PageText
    
    
    
    
        For Each oBook In BookLibrary
            s = s & oBook.BookTitle & vbCrLf
        Next
        
        MsgBox s
    
    
        s = ""
        For Each oPage In BookLibrary("Book Title 3").BookPages
            s = s & oPage.PageNumber & " -- " & oPage.PageText & vbCrLf
        Next
        
        MsgBox s
    End Sub

    clsBook:

    Option Explicit
    
    
    Private m_Title As String
    Private m_Pages As Collection
    
    
    Private Sub Class_Initialize()
        Set m_Pages = New Collection
    End Sub
    
    
    Private Sub Class_Terminate()
        Set m_Pages = Nothing
    End Sub
    
    
    Property Get BookTitle() As String
        BookTitle = m_Title
    End Property
    Property Let BookTitle(s As String)
        m_Title = s
    End Property
    
    
    Property Get BookPages() As Collection
        Set BookPages = m_Pages
    End Property
    
    
    Property Get BookPage(Nbr As Long) As clsPage
        Set BookPage = m_Pages(CStr(Nbr))
    End Property
    clsPage:

    Option Explicit
    
    
    Private m_PageNumber As Long
    Private m_PageText As String
    
    
    Property Get PageNumber() As Long
        PageNumber = m_PageNumber
    End Property
    Property Let PageNumber(n As Long)
        m_PageNumber = n
    End Property
    
    
    Property Get PageText() As String
        PageText = m_PageText
    End Property
    Property Let PageText(s As String)
        m_PageText = s
    End Property

    If you just wanted to add pages sequentially, then maybe something like this

            For j = 1 To 5            Set oPage = New clsPage
                oPage.PageNumber = oBook.BookPages.Count + 1
                oPage.PageText = "This is text for " & oBook.BookTitle & " Page " & CStr(oPage.PageNumber)
                Call oBook.BookPages.Add(oPage, CStr(oPage.PageNumber))
            Next j
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Falcontr,

    That's a bad example.

    ClsChapter
    Private pChapTitle as String
    
    'How to read the chapter Title
    Public Property Get Title() As String
       Title = pChapTitle
    End Property
    
    'How to write the chapter Title
    Public Property Let Title(Chapter_Title As String)
       pChapTitle = Chapter_Title
    End Property)
    ClsBook
    Private pChapters As Collection
    Private pBook_Title As String
    
    'Write the Book's Title
    Public Property Let Title(BookTitle) As String
       pBook_Title = Title
    End Property
    
    'Read the Book's Title
    Public Property Get Title() As String
       Title = pBook_Title
    End Property
    
    'Add a Chapter to the Book
    Public Function AddChapter(ChapterTitle As String)
       Dim X as New ClsChapter
       X.Title = ChapterTitle
       pChapters.Add X
       set X = Nothing
    End Function
    
    'How many Chapters in Book
    Public Property Get Count() As long
       Count = pChapters.Count
    End Property
    
    'What is Particular Chapter Title
    Public Property Get ChapterTitle(ChapterNumber As long) As String
       ChapterTitle = pChapters(ChapterNumber).Title
    End Property
    Classes should only interact with the outside via Public Properties and Methods (Functions and Subs). The convention is that Properties only read and write Variables, and Methods perform actions. Another convention is that Class Variable used by Properties are prefixed with a special character. I use "p" for Property usage and "m" for Method usage, YMMV. The "Me" Keyword allows the internal use of Public Properties and Methods
    Last edited by SamT; 03-27-2021 at 07:22 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

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
  •