Text to HTML Learn programming Visual Basic (VB.net)

Lesson:

OOP More On Classes


Exercise:

Text to HTML


Objetive:

Create a class "TextToHTML", which must be able to convert several texts entered by the user into a HTML sequence, like this one:

Hola
Soy yo
Ya he terminado

should become

Hola

Soy yo

Ya he terminado

The class must contain:
An array of strings
A method "Add", to include a new string in it
A method "Display", to show its contents on screen
A method "ToString", to return a string containing all the texts, separated by "\n".
Create also an auxiliary class containing a "Main" function, to help you test it.


Code:

Imports System
Class TextToHTML
    Protected myHTML As String()
    Protected maxLines As Integer = 1000
    Private counter As Integer = 0

    Public Sub New()
        myHTML = New String(maxLines - 1) {}
    End Sub

    Public Sub Add(ByVal newSentence As String)
        If counter < maxLines Then
            myHTML(counter) = newSentence
            counter += 1
        End If
    End Sub

    Public Function ToString() As String
        Dim allHTML As String = vbLf & vbLf

        For i As Integer = 0 To counter - 1
            allHTML += myHTML(i)
            allHTML += vbLf
        Next

        allHTML += vbLf
        allHTML += vbLf
        Return allHTML
    End Function

    Public Sub Display()
        Console.Write(ToString())
    End Sub
End Class

Class TextTest
    Private Shared Sub Main(ByVal args As String())
        Dim example As TextToHTML = New TextToHTML()
        example.Add("Hola")
        example.Add("uno dos")
        example.Add("tres cuatro")
        example.Display()
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.