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
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