Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 44,084 times

Contents

Related Categories

Polymorphism in VB - Using it (2)

Using it (2)

Below is the code for the cText2HTML class...

Code (cText2HTML.cls)

Option Explicit
Implements IConvert
Private m_sOutput As String
Private m_sSource As String
'sets the source text...
Private Property Let IConvert_SourceText(RHS As String)
    'set a new value
    m_sSource = RHS
End Property
Private Property Get IConvert_SourceText() As String
    'get the current value
    IConvert_SourceText = m_sSource
End Property
'returns the outputted text...
Private Property Get IConvert_OutputText() As String
    IConvert_OutputText = m_sOutput
End Property
'the conversion procedure
Private Function IConvert_Convert() As Boolean
    'pretend to convert text to HTML...
    'just add a HTML header and footer + pre tags
    Dim lEndPos As Long
    Dim sTitle As String
    m_sOutput = ""
    'get the title text from the first line
    'get the end of the line
    lEndPos = InStr(1, m_sSource, vbCrLf)
    If lEndPos <> 0 Then
        'return the text in between the title tag
        sTitle = Left$(m_sSource, lEndPos - 1)
    End If
    'add the HTML header
    m_sOutput = "<html><head><title>" & sTitle & "</title></head>" & vbCrLf & "<body>"
    'add the rest of the text
    m_sOutput = m_sOutput & Mid$(m_sSource, lEndPos + 2, Len(m_sSource) - (lEndPos + 1))
    m_sOutput = m_sOutput & "</body></html>"
    IConvert_Convert = True
End Function

 

Now that we have created the two conversion classes, we need to write some code that uses them. First, add the following controls to a form...

Controls (frmExample.frm)
Type Name Properties
ComboBox cboConvert Style=3,List='HTML to Text'+'Text to HTML'
CommandButton cmdConvert Caption=Convert
TextBox txtSource Multiline=True
TextBox txtResult Multiline=True

and then add the code below.

Code (cText2HTML.cls)

Option Explicit

Private Sub cmdConvert_Click()
    Dim cConvert As IConvert
    Select Case cboConvert.ListIndex
    Case 0 'html2text
        Set cConvert = New cHTML2Text
    Case 1 'text2html
        Set cConvert = New cText2HTML
    End Select
    cConvert.SourceText = txtSource.Text
    'convert
    If cConvert.Convert = False Then
        MsgBox "Conversion failed!", vbCritical
    Else
        txtResult.Text = cConvert.OutputText
    End If
End Sub

Private Sub Form_Load()
    cboConvert.ListIndex = 1
End Sub

Notice that in the Select...Case statement, even though cConvert is set to two different types of class (cHTML2Text or cText2HTML), cConvert does not have to be declared as an Object. Instead, it is declared as IConvert, because both the classes use that Interface. This is one of the major benefits of using polymorphism. However, after doing all this, and running your project, you might still say, “so what” so, in the next section, I will try to convince you of its other benefits!

** You can download all the source code used and the example project by clicking here **

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Posted by James Crowley on 28 Dec 2004

    If you need to access methods specific to that object, then you need to declare a variable for that object. For instance,

    Dim obj As myActualObject

    Set obj = New myActualObject

    rather than t...

  • Yay

    Posted by phracker76 on 02 Jan 2004

    good stuff this

  • Methods in the Implementing Class

    Posted by Sparvhok on 22 Oct 2003

    Hi there,

    Great article, very interesting. But when I tried to implement the idea it worked well until. When trying to create public methods in the Implementing Classes of the Interface other than ...