Overriding a property is not working.

D

Dom

I guess I don't really understand how "override" works with
properties. For various reasons, I've created a JournalListBox
control that inherits ListBox. This control has a List of
"Journals" (over 6,000 items), but the journals do not show up in the
listbox. Instead, as the user types, the list box shows a set of only
12 appropriate items. For example, if the user type "J", then the
JournalListBox shows the first 12 items that begin with "J", and so
on.

I decided to override the SelectedIndex property. The user can set
the SelectedIndex to, say, 4397. The JournalListBox translates this
into the appropriate number between 0 and 11, then sets that number.
Here is how it looks:
----------------------------
public override int SelectedIndex
{
get
{
if (base.SelectedIndex == -1) { return -1; }
else { return (base.SelectedIndex + StartIndex); }
}
set
{
if (value == -1) { base.SelectedIndex = -1; }
else { base.SelectedIndex = (value - StartIndex); }
}
}
----------------------

"StartIndex" is, of course, the index into the Datalist that appears
as the first item in the listbox.

Some problems:
1. I know from debugging that when the setter is called, it in turn
calls the getter. Why?
2. If in the immediate window I request base.SelectedItem, it calls
the getter. Why? I thought it would only call the getter if I
requested this.SelectedItem.
3. Since base.SelectedItem calls the getter, and the getter calls
base.SelectedItem, why am I not caught in an infinite loop?

TIA,
Dom
 
D

Dom

I guess I don't really understand how "override" works with
properties.  For various reasons, I've created a JournalListBox
control that inherits ListBox.  This control has a List of
"Journals" (over 6,000 items), but the journals do not show up in the
listbox.  Instead, as the user types, the list box shows a set of only
12 appropriate items.  For example, if the user type "J", then the
JournalListBox shows the first 12 items that begin with "J", and so
on.

I decided to override the SelectedIndex property.  The user can set
the SelectedIndex to, say, 4397.  The JournalListBox translates this
into the appropriate number between 0 and 11, then sets that number.
Here is how it looks:
----------------------------
        public override int SelectedIndex
        {
            get
            {
                if (base.SelectedIndex == -1) { return -1; }
                else { return (base.SelectedIndex + StartIndex); }
            }
            set
            {
                if (value == -1) { base.SelectedIndex= -1; }
                else { base.SelectedIndex = (value - StartIndex); }
            }
        }
----------------------

"StartIndex" is, of course, the index into the Datalist that appears
as the first item in the listbox.

Some problems:
1.  I know from debugging that when the setter is called, it in turn
calls the getter.  Why?
2.  If in the immediate window I request base.SelectedItem, it calls
the getter.  Why?  I thought it would only call the getter if I
requested this.SelectedItem.
3.  Since base.SelectedItem calls the getter, and the getter calls
base.SelectedItem, why am I not caught in an infinite loop?

TIA,
Dom

As a test, I decided to change "override" to "new". It works now.
This makes me think that the error is occuring somewhere else, like in
the designer for the JournalListBox class. Am I right to think that
"override" should work for me, and is the preferred approach?
 
D

Dom

As a test, I decided to change "override" to "new".  It works now.
This makes me think that the error is occuring somewhere else, like in
the designer for the JournalListBox class.  Am I right to think that
"override" should work for me, and is the preferred approach?- Hide quoted text -

- Show quoted text -

Does it matter that JournalListBox is not a UserControl, but is a
Custom Control? If so, I have to admit, I never knew the difference
between the two. I always used the rule : A UserControl is one that
has several components, eg, a text box and a listbox; and a Custom
Control is a control that has new methods and overrides.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top