How do you override a virtual property in a generic class?

G

Geoff

....Or, to put it another way, why doesn't something like this work?

public class RecordCollection : List<Record>
{
//Some code snipped
public override int Count
{
get
{
return someCustomCount;
}
}
}

The error the compiler gives is 'cannot override inherited member
'System.Collections.Generic.List<OpinionatedGeek.Applications.Alchemy.Record>.Count.get'
because it is not marked virtual, abstract, or override'

That may well be true, but
'System.Collections.Generic.List<>.Count.get' _is_ marked virtual. How
do I go about overriding it? Or is it just not possible?

Any ideas? Or any better places to post this question?

Many thanks,

Geoff
 
N

Nicholas Paldino [.NET/C# MVP]

Geoff,

The reason you are getting this is because the Count property is not
virtual (I have beta 2, and I am looking at it in Reflector), so you can't
use the override keyword on it. You would have to use the new keyword to
shadow the property.

Hope this helps.
 
G

Geoff

That's... annoying.

Still, it'll teach me not to rely on the documentation - it says:

Syntax
Visual Basic
Public Overridable ReadOnly Property Count() As Integer

C#
public virtual int Count {get;}

(I don't think a shadowed property will work in this case - the access
will be done through a base-class reference, so it won't see the 'new'
accessor. I'll just have to figure out something else.)

Many thanks for your help,

Geoff
 
N

Nicholas Paldino [.NET/C# MVP]

Geoff,

You are right, it won't work in this case. I'm curious though, why
would you need to override the Count property? It's pretty basic in what it
does, and I can't see why you would want to change it.
 
G

Geoff

It was just one of the ways I was thinking about fiddling with
pagination in a GridView, that's all.

The DataGrid can use a VirtualItemCount property, but the GridView
can't, so I was going to implement a smart collection that could mask
the fact that it was incomplete and manage its own dynamic loading of
record sets behind the scenes. So 'Count' would list the total number
of records available, even though only, say, records 20-30 were
retrieved from the database for this page view.

There are a bunch of other ways of doing this, but this one was simple
and I could do it without putting any code on the page.

Cheers,

Geoff
 
B

Bill Butler

Geoff said:
C#
public virtual int Count {get;}

(I don't think a shadowed property will work in this case - the access
will be done through a base-class reference, so it won't see the 'new'
accessor. I'll just have to figure out something else.)

Why exactly would you want the Count property of a list to not be the number of elements in the
list?
Perhaps you want it to return the number of elements meeting some condition?

Just Curious
Bill
 
N

Nicholas Paldino [.NET/C# MVP]

Geoff,

I guess the other way to do this would be to create a wrapper class
which implements the appropriate interface. This way, you can override the
Count property in the manner you wish. All you have to do is pass the
original list to the new implementation (kind of like dataview and
datatable).
 
B

Bill Butler

Geoff said:
The DataGrid can use a VirtualItemCount property, but the GridView
can't, so I was going to implement a smart collection that could mask
the fact that it was incomplete and manage its own dynamic loading of
record sets behind the scenes. So 'Count' would list the total number
of records available, even though only, say, records 20-30 were
retrieved from the database for this page view.

There are a bunch of other ways of doing this, but this one was simple
and I could do it without putting any code on the page.

You Could Use Composition instead of inheritance to achieve this.
Of course you would need to provide all of the plumbing yourself.

Of course you probably wouldn't need to expose ALL of the methods in List<>

Bill
 
G

Geoff

Yeah, that's pretty much my next plan. I've done it before, and it's
not a heap of work, I just thought specialising the generic List base
class would be a bit quicker with less code to maintain.

But you're right, composition will give me all the control I need.

Many thanks,

Geoff
 

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