LaTeX-Report Engine OOP

Good Morning!

I startet to create a Report-Engine to generate LaTeX-Documents.

The Main-Class LaTeXReport has a Dictionary called Pages.

The Dictionary Pages can store LaTeXPages. But there will be different SubClasses of a LaTeXPage:

[h]SubClasses of LaTeXPage[/h]
LaTeXPersonSheet
LaTeXRelationshipSheet…

How can i store different SubClasses of LaTeXPage and get the individual SubClass SaveLatex-Output?

Link to the Classes

Best regards!

Hi Martin,

If you create an array and make it the type of your class you can have an array of those subclasses. This will allow you to move through each array element (each subclass) and decide which one to use.

HTH.

is similar cases, I do something like this:

[code]
BaseClass
SubClass1 (of BaseClass)
SubClass2 (of BaseClass)

dim b as BaseClass
for each b in Pages.Values
if b isA SubClass1 then
dim s as SubClass1 = SubClass1(b) ’ cast to SubClass1
’ now I can use all specific properties of SubClass1

end if
if b isA SubClass2 then
dim s as SubClass2 = SubClass2(b) ’ cast to SubClass2
’ now I can use all specific properties of SubClass2

end if
next[/code]

Let’s say you have BaseClass, SubClassA and SubClassB. If I understand correctly, you want to do something like:

Dim classes() As BaseClass
BaseClass.Append New SubClassA
BaseClass.Append New SubClassB

... later ...

For Each c As BaseClass In classes
  c.WriteData(myTextOutputStream)
Next

? If so, simply store references to your sub classes as the base class, and make a method in the base class called WriteData or whatever, then override it in your subclasses. You could also achieve the same thing by using events. Have the BaseClass raise an event that theSubClass implements.

Hello Everyone,

thank you very much for your input. I created a smaller Structure, to go Step by Step. Now the TeXReport.SaveTeX-Typecast works fine!

[h]Example[/h]

[code]Dim Report As New TeXReport
Dim Page As TeXPage

Page = New TeXPageDog(3)
Page.ID = 1
Report.AddPage Page

Page = New TeXPageCat(True)
Page.ID = 2
Report.AddPage Page

Page = New TeXPageCat(False)
Page.ID = 3
Report.AddPage Page

Page = New TeXPageDog(1)
Page.ID = 4
Report.AddPage Page

TextArea1.Text = Report.SaveTeX?[/code]

Here are code of my Class-Structure. The only thing is, i wanna save and load the Report(s) as a BinaryStream. How can i load and add the Pages correct? I marked this at the source, where i have the question b[/b]! How to load the Pages correctly into the Pages-Dictionary?

Thanks for your time and Feedback!

[h]Class TeXReport[/h]

[code]Pages As Dictionary

Sub AddPage(APage As TeXPage)
Select Case APage
Case IsA TeXPageCat
Pages.Value(TeXPageCat(APage).ID) = TeXPageCat(APage)
Case IsA TeXPageDog
Pages.Value(TeXPageDog(APage).ID) = TeXPageDog(APage)
End Select
End Sub

Sub constructor()
Pages = New Dictionary
End Sub

Sub Load(s As BinaryStream)
Dim PagesCount As Integer = s.ReadInt32
For i As Integer = 0 To PagesCount
'how to Load???
Next
End Sub

Sub Save(s As BinaryStream)
s.WriteInt32(Int32(Pages.Count))
For Each Page As TeXPage In Pages.Values
Select Case Page
Case IsA TeXPageCat
TeXPageCat(Page).Save(s)
Case IsA TeXPageDog
TeXPageDog(Page).Save(s)
End Select
Next
End Sub

Function SaveTeX() As String
Dim TeX As String
TeX = “TeX-Document” + EndOfLine + EndOfLine
For Each Page As TeXPage In Pages.Values
Select Case Page
Case IsA TeXPageCat
TeX = TeX + TeXPageCat(Page).SaveTeX
Case IsA TeXPageDog
TeX = TeX + TeXPageDog(Page).SaveTeX
End Select
Next
TeX = TeX + EndOfLine + “End of TeX-Document” Return TeX
End Function[/code]

[h]Class TeXPage[/h]

[code]ID As Integer

Sub Load(s As BinaryStream)
ID = s.ReadInt32
End Sub

Sub Save(s As BinaryStream)
s.WriteInt32(Int32(ID))
End Sub[/code]

[h]Class TeXPageCat[/h]

[code]Inherits TeXPage

LikesMilk As Boolean

Sub constructor(Milk As Boolean)
LikesMilk = Milk
End Sub

Sub Load(s As BinaryStream)
Super.Load(s)
LikesMilk = s.ReadBoolean
End Sub

Sub Save(s As BinaryStream)
Super.Save(s)
s.WriteBoolean(LikesMilk)
End Sub

Function SaveTeX() As String
If LikesMilk Then
Return "Cat " + CStr(ID) + “, she likes Milk” + EndOfLine
Else
Return "Cat " + CStr(ID) + “, she don’t like Milk” + EndOfLine
End If
End Function[/code]

[h]Class TeXPageDog[/h]

[code]Inherits TeXPage

Age As Integer

Sub constructor(AAge As Integer)
Age = AAge
End Sub

Sub Load(s As BinaryStream)
Super.Load(s)
Age = s.ReadInt32
End Sub

Sub Save(s As BinaryStream)
Super.Save(s)
s.WriteInt32(Int32(Age))
End Sub

Function SaveTeX() As String
Return "Dog " + CStr(ID) + " , Age: " + CStr(Age) + EndOfLine
End Function[/code]

Ok, i found a solution by myself.

[h]Changes at TeXReport[/h]

Load

[code]Dim PagesCount As Integer = s.ReadInt32

For i As Integer = 0 To PagesCount
Dim Type As Integer = s.ReadInt32
Dim Page As TeXPage
Select Case Type
Case 1
Page = New TeXPageCat
Page.Load(s)
AddPage(Page)
Case 2
Page = New TeXPageDog
Page.Load(s)
AddPage(Page)
End Select
Next[/code]

Save

[code]s.WriteInt32(Int32(Pages.Count))

For Each Page As TeXPage In Pages.Values
s.WriteInt32(Int32(Page.Type))
Select Case Page
Case IsA TeXPageCat
TeXPageCat(Page).Save(s)
Case IsA TeXPageDog
TeXPageDog(Page).Save(s)
End Select
Next[/code]

Thanks for Feedback!