• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Inheritance

I have the following vb.net code:

Public Class CSipLine
    Inherits CPhoneLine

    Property SIPDomain as string

end class

---------------------------------------

Public Class CPhoneLine
 
    Property AccountBalance as decimal

end class

---------------------------
now i would like to be able to do something like this:

Dim PhoneLine as new CPhoneLine

PhoneLine.Balance = 10

Dim SIPLine as new CSIPLine

SIPLine = PhoneLine

SIPLine.SIPDomain = "test"

------------------------------------
this code gets an error, saying invalid type cast.

what i want to achieve is assign a SIPLine object with a PhoneLine object, and essentially copy all data from phoneline into sipline object.  how can this be done?

thanks.

0
xorcrack
Asked:
xorcrack
  • 3
1 Solution
 
S-TwilleyCommented:
Which of the two classes is the "simple" one... from which the other is based (inherits it's properties from)?
0
 
S-TwilleyCommented:
Ahhh sorry... i missed a line there... ignore my last comment
0
 
Brian CroweDatabase AdministratorCommented:
You could create a Copy procedure in CSipLine that takes a CPhoneLine parameter and copy the appropriate members.
0
 
xorcrackAuthor Commented:
thats what i was thinking about.  i was just wondering if there was a more elegant way?
0
 
S-TwilleyCommented:
A cheap work around (if there is no other way other than passing the sub class as a parameter)... would be not to use inherittance and use a property instead:

Public Class SimpleClass
    Private _SomeText As String

    Public ReadOnly Property SomeText() As String
        Get
            Return _SomeText
        End Get
    End Property
End Class

Public Class ComplexClass
    Public BaseClass As SimpleClass

    Public Sub New()
        BaseClass = New SimpleClass
    End Sub

    Public Sub New(ByVal PassedBase As SimpleClass)
        BaseClass = PassedBase
    End Sub

    Public ReadOnly Property SomeText() As String
        Get
            Return BaseClass.SomeText
        End Get
    End Property
End Class

===================

The ComplexClass.SomeText property  is not required... as it is accessible  through  ComplexClass.BaseClass.SomeText .... but just a quicker way of accessing it
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now