Solved

Overriding a property

Posted on 2004-10-05
5
265 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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