Solved

Overriding a property

Posted on 2004-10-05
5
261 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
ID: 12230510
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
ID: 12230564
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
ID: 12230777
thanks, but just one quick question.
Why Friend instead of private? It works if i leave it as private...
0
 

Author Comment

by:rpter
ID: 12230813
doesnt matter, just realised!
Thanks again
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12230826
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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