View Full Version : Nested classes with dynamic array
falcontra
03-26-2021, 02:32 PM
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
Paul_Hossler
03-26-2021, 05:30 PM
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
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
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.