?
Solved

Overriding a property

Posted on 2004-10-05
5
Medium Priority
?
267 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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