Viktor M.
Viktor M.

Reputation: 4623

VB: Problems with using variable from another class + what to do with not used interface`s functions

I have a problem with getting variable from another class and cannot understand what to do with interface`s functions which have already existed in another class.

What I have:

Form where clicking on a button I should see reversed string: (I want to call pooraja.StringReverse which is below)

 Private Sub btnPoora1_Click(sender As System.Object, e As System.EventArgs) _
        Handles btnPoora1.Click

        'Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
        Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
        Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja

        text.strText = txtSisendTekst.Text
        txtValjundTekst1.Text = pooraja.stringReverse

        text.intStart = 1
        text.intEnd = Len(txtSisendTekst.Text)

        ascFSymbol.Text = text.ascFirstSymbol
        ascLSymbol.Text = text.ascLastSymbol()
    End Sub

CtekstiPooraja: (Thiss class will be used to store data.Under data I mean strPooratavText. Data will be used in CAlgoritmilinePooraja)

    Public Class CtekstiPooraja
    Implements ITeisendused

    Public intStartSymbol As Integer
    Public intEndSymbol As Integer
    Public strPooratavText As String

    Private Property intEnd As Integer Implements ITeisendused.intEnd
        Get
            Return intEndSymbol
        End Get
        Set(ByVal value As Integer)
            intEndSymbol = value
        End Set
    End Property

    Private Property intStart As Integer Implements ITeisendused.intStart
        Get
            Return intStartSymbol
        End Get
        Set(ByVal value As Integer)
            intStartSymbol = value
        End Set
    End Property

    Public Function pooraText() As String Implements ITeisendused.pooraText
        Return StrReverse(strPooratavText)
    End Function

    Public Property strText As String Implements ITeisendused.strText
        Get

            Return strPooratavText
        End Get
        Set(ByVal value As String)
            strPooratavText = value
            MsgBox(strPooratavText)
        End Set
    End Property

    Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
        strPooratavText = StrReverse(strSisendText)
    End Sub

    Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
        Return Asc(GetChar(strPooratavText, intStartSymbol))
    End Function

    Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
        Return Asc(GetChar(strPooratavText, intEndSymbol))
    End Function

    Public Function stringReverse() As String Implements ITeisendused.stringReverse
        Return Nothing
    End Function



End Class

CAlgrotimilinePooraja: (This class will be called by form button. There I need to use stringReverse function with data from CtekstiPooraja. The problem is that everywhere is used the same interface and there is some functions and procedures from this interface which isnt necessary. I dont know what value should return these unused functions/procedures. Just using "return Nothing or return 0/ "" is bad idea, may be there is possible somehow referenceto to CTekstiPooraja functions/procedures variables")

Public Class CAlgrotimilinePooraja
    Implements ITeisendused

    Private x As New PrjTekstiPooraja.CtekstiPooraja
    Public Function stringReverse() As String Implements ITeisendused.stringReverse
        MsgBox(x.strPooratavText)
        Dim i As Integer = 0
        Dim j As Integer
        Dim characters(j) As Char
        Dim newString(j) As Char
        characters = x.strPooratavText.ToCharArray()
        newString = x.strPooratavText.ToCharArray()

        Do While i <= j - 1
            newString(i) = characters(j - 1)
            newString(j - 1) = characters(i)
            i += 1
            j -= 1
        Loop
        Return newString
    End Function

    Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
        Return x.ascFirstSymbol()
    End Function

    Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
        Return Nothing
    End Function

    Public Property intEnd As Integer Implements ITeisendused.intEnd
        Get
            Return x.intEndSymbol
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property intStart As Integer Implements ITeisendused.intStart
        Get
            Return x.intStartSymbol
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Function pooraText() As String Implements ITeisendused.pooraText
        Return x.pooraText()
    End Function

    Public Property strText As String Implements ITeisendused.strText
        Get
            Return x.strPooratavText
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
        x.strPooratavText = StrReverse(strSisendText)
    End Sub
End Class

MyInterface:

Public Interface ITeisendused

    Property intStart As Integer
    Property intEnd As Integer
    Property strText As String

    Function pooraText() As String
    Function ascFirstSymbol() As String
    Function ascLastSymbol() As String
    Function stringReverse() As String
    Sub teisendaText(ByRef strSisendText As String)

End Interface

I cannot understand how to get variable strPooratavText from CTekstiPooraja to CAlgrotimilinePooraja. Usually that instancewhich I create worked but not now. And I cannot understand what to do with already existed function and procedures in CAlgoritmilinePooraja when the same function and procedures has in another class. Maybe, it is possible to reference them somehow to existed functions/procedures in CTekstiPooraja? Could you explain me how to id, already tired to surf Internet to find a solution for it, have already try a lot.

Upvotes: 0

Views: 761

Answers (1)

jmoreno
jmoreno

Reputation: 13571

Well, I think you have a fundamental problem with understanding interfaces. They describe data and behavior, it should be extremely rare to want to implement part of an interface.

That said, if you do want to implement part of an interface, instead of returning bogus data, throw an exception for behavior you don't implement.

Your specific problem is that CAlgoritmilinePooraja works on an instance of CtekstiPooraja, but it creates a new instance instead of using an existing one. Add

Sub New(incomingX as CtekstiPooraja)
  x = incomingX
End Sub

to CAlgoritmilinePooraja. And then in your event, use....

Dim text As PrjTekstiPooraja.CtekstiPooraja = New PrjTekstiPooraja.CtekstiPooraja
text.strText = txtSisendTekst.Text
Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja(text)

That is the minimum change to your design that gets what you want to happen to happen but it's problably not what you should do. Other than implementing strReverse, CtekstiPooraja seems to be what you want, CAlgrotimilinePooraja looks to do just one thing, the actual string reversal.

I would move the implementation of strReverse into CtekstiPooraja, and then eliminate CAlgrotimilinePooraja.

PS I would try to stick to English for class names as well as functions and variables.

Upvotes: 1

Related Questions