Solved

Overriding a property

Posted on 2004-10-05
5
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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