Solved

Overriding a property

Posted on 2004-10-05
5
259 Views
Last Modified: 2010-04-23
I've written 2 classes, the second one inherits from the first class. In the first class i have a property that has been declared as overridable.
I've created the property in the second class, but everytime i try to use it i get the error: An unhandled exception of type 'System.StackOverflowException' occurred in test1.exe

The original classes are rather large to post here, but i've created a small test program that does exactly the same; heres the code

1st Class
------------
Public Class Item

    ' private members
    Private m_ItemName As String


    ' property
    Public Overridable Property ItemName() As String
        Get
            Return m_ItemName
        End Get
        Set(ByVal Value As String)
            m_ItemName = Value
        End Set
    End Property

End Class

2nd Class
-----------

Public Class BiggerItem
    Inherits Item

    ' another prrivate member
    Private m_ItemDescription As String

    Public Property ItemDescription() As String
        Get
            Return m_ItemDescription
        End Get
        Set(ByVal Value As String)
            m_ItemDescription = Value
        End Set
    End Property

    Public Overrides Property ItemName() As String
        Get
            Return Me.ItemName
        End Get
        Set(ByVal Value As String)
            Me.ItemName = Value
        End Set
    End Property


End Class

now on a form i've dropped a button, with the following code

dim example1 as new BiggerItem
example1.ItemName = "Argh!"

As soon as it hits the second line i get the error, but i dont know hwo to fix it.
Any help appreciated.

Thanks

Roland
0
Comment
Question by:rpter
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Hi Roland,

it shouldn't be me but mybase

like

Public Overrides Property ItemName() As String
        Get
            Return MyBase.m_ItemName
        End Get
        Set(ByVal Value As String)
            MyBase.m_ItemName = Value
        End Set
    End Property
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 250 total points
Comment Utility
sorry forgot one thing

this

Private m_ItemName As String

should be

Friend m_ItemName As String

or you could return the property of the base class (which is better) like

Public Class Item2

    ' private members
    Private m_ItemName As String


    ' property
    Public Overridable Property ItemName() As String
        Get
            Return m_ItemName
        End Get
        Set(ByVal Value As String)
            m_ItemName = Value
        End Set
    End Property

End Class


Public Class BiggerItem
    Inherits Item2

    ' another prrivate member
    Private m_ItemDescription As String

    Public Property ItemDescription() As String
        Get
            Return m_ItemDescription
        End Get
        Set(ByVal Value As String)
            m_ItemDescription = Value
        End Set
    End Property

    Public Overrides Property ItemName() As String
        Get
            Return MyBase.ItemName
        End Get
        Set(ByVal Value As String)
            MyBase.ItemName = Value
        End Set
    End Property
End Class




0
 

Author Comment

by:rpter
Comment Utility
thanks, but just one quick question.
Why Friend instead of private? It works if i leave it as private...
0
 

Author Comment

by:rpter
Comment Utility
doesnt matter, just realised!
Thanks again
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Hi rpter, it will work in the last example I gave you.

it won't work when you do this (because m_itemname is not exposed to mybase if it is private)

Public Overrides Property ItemName() As String
        Get
            Return MyBase.m_ItemName
        End Get
        Set(ByVal Value As String)
            MyBase.m_ItemName = Value
        End Set
    End Property
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now