There's something I don't understand about how the CollectionBase class is
defined. I can declare:
class Derived : CollectionBase
{
}
I can instantiate this Derived class:
Derived derived = new Derived();
I cannot call an Add method as I haven't declared it:
derived.Add(1); //compiler error: Derived does not contain a definition
for Add
However CollectionBase derives from IList, IList declares Add as public
abstract, and I can successfully call Add through the IList interface:
IList ilist = derived;
ilist.Add(1);
So what is happening here:
* If neither CollectionBase nor Derived define Add, then why can I
instantiate Derived, and call Add through the IList interface?
* Or if CollectionBase does implement Add, then why can't I see it in the
Object browser and why do I get an error when I try to call it?
I understand how I'm *supposed* to use CollectionBase (I'm supposed to
define an Add method in a derived class). What I don't understand is how
CollectionBase itself is is declared or implemented: how is it that a
Derived : CollectionBase can derive from IList, and be instantiable, when
the Add method isn't defined or implemented in Derived nor in
CollectionBase, but is declared as abstract in IList? 7 2747
Well Add isn't really instantiable via Derived (because of Abstract) and
hence doesn't appear
to be available however you cheated when trying to access it via the
Interface. As far as I know
interfaces simply tell you whats available - if the programmer didn't
actually impliment the method correcly
the interface won't know. What happens if you run this code, does it crash
when calling Add? It will probably
just do nothing as Add is in the base class and does nothing - it might be
there but its probably empty.
C http://www.typemismatch.com/
(For Developers!)
"Christopher Wells" <ch***************@invalid.invalid> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... There's something I don't understand about how the CollectionBase class is defined. I can declare:
class Derived : CollectionBase { }
I can instantiate this Derived class:
Derived derived = new Derived();
I cannot call an Add method as I haven't declared it:
derived.Add(1); //compiler error: Derived does not contain a
definition for Add
However CollectionBase derives from IList, IList declares Add as public abstract, and I can successfully call Add through the IList interface:
IList ilist = derived; ilist.Add(1);
So what is happening here:
* If neither CollectionBase nor Derived define Add, then why can I instantiate Derived, and call Add through the IList interface?
* Or if CollectionBase does implement Add, then why can't I see it in
the Object browser and why do I get an error when I try to call it?
I understand how I'm *supposed* to use CollectionBase (I'm supposed to define an Add method in a derived class). What I don't understand is how CollectionBase itself is is declared or implemented: how is it that a Derived : CollectionBase can derive from IList, and be instantiable, when the Add method isn't defined or implemented in Derived nor in CollectionBase, but is declared as abstract in IList?
Christopher Wells <ch***************@invalid.invalid> wrote: There's something I don't understand about how the CollectionBase class is defined.
It's called explicit interface implementation - it's quite bizarre, to
be honest.
See http://www.jaggersoft.com/csharp_standard/20.4.1.htm for more
information.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
ilPostino <ne**@ip80.com> wrote: Well Add isn't really instantiable via Derived (because of Abstract) and hence doesn't appear to be available however you cheated when trying to access it via the Interface. As far as I know interfaces simply tell you whats available - if the programmer didn't actually impliment the method correcly the interface won't know. What happens if you run this code, does it crash when calling Add? It will probably just do nothing as Add is in the base class and does nothing - it might be there but its probably empty.
No, it's there all right - it validates the value and then adds it to
the list.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
when you explicitly implement an interface method, that method is not accessible through the class itself, only accessible through casting to the interface
----- ilPostino wrote: ----
Well Add isn't really instantiable via Derived (because of Abstract) an
hence doesn't appea
to be available however you cheated when trying to access it via th
Interface. As far as I kno
interfaces simply tell you whats available - if the programmer didn'
actually impliment the method correcl
the interface won't know. What happens if you run this code, does it cras
when calling Add? It will probabl
just do nothing as Add is in the base class and does nothing - it might b
there but its probably empty http://www.typemismatch.com
(For Developers!
"Christopher Wells" <ch***************@invalid.invalid> wrote in messag
news:%2****************@TK2MSFTNGP09.phx.gbl.. There's something I don't understand about how the CollectionBase class i defined. I can declare class Derived : CollectionBas
I can instantiate this Derived class Derived derived = new Derived() I cannot call an Add method as I haven't declared it derived.Add(1); //compiler error: Derived does not contain
definitio for Ad However CollectionBase derives from IList, IList declares Add as publi abstract, and I can successfully call Add through the IList interface IList ilist = derived ilist.Add(1) So what is happening here * If neither CollectionBase nor Derived define Add, then why can instantiate Derived, and call Add through the IList interface * Or if CollectionBase does implement Add, then why can't I see it i
th Object browser and why do I get an error when I try to call it I understand how I'm *supposed* to use CollectionBase (I'm supposed t define an Add method in a derived class). What I don't understand is ho CollectionBase itself is is declared or implemented: how is it that Derived : CollectionBase can derive from IList, and be instantiable, whe the Add method isn't defined or implemented in Derived nor i CollectionBase, but is declared as abstract in IList
What you see is whats called an explicit interface. The idea is to hide
the various interfaces from the developer unless you *explicitly* decide
to use the interface method. Its a language design choice to help
*users* of a particular class focus only on interfaces that would be
useful to them (from the perspective of the *provider* ofcourse)
Christopher Wells wrote: There's something I don't understand about how the CollectionBase class is defined. I can declare:
class Derived : CollectionBase { }
I can instantiate this Derived class:
Derived derived = new Derived();
I cannot call an Add method as I haven't declared it:
derived.Add(1); //compiler error: Derived does not contain a definition for Add
However CollectionBase derives from IList, IList declares Add as public abstract, and I can successfully call Add through the IList interface:
IList ilist = derived; ilist.Add(1);
So what is happening here:
* If neither CollectionBase nor Derived define Add, then why can I instantiate Derived, and call Add through the IList interface?
* Or if CollectionBase does implement Add, then why can't I see it in the Object browser and why do I get an error when I try to call it?
I understand how I'm *supposed* to use CollectionBase (I'm supposed to define an Add method in a derived class). What I don't understand is how CollectionBase itself is is declared or implemented: how is it that a Derived : CollectionBase can derive from IList, and be instantiable, when the Add method isn't defined or implemented in Derived nor in CollectionBase, but is declared as abstract in IList?
--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Christopher Wells <ch***************@invalid.invalid> wrote: There's something I don't understand about how the CollectionBase class
is defined.
It's called explicit interface implementation - it's quite bizarre, to be honest.
See http://www.jaggersoft.com/csharp_standard/20.4.1.htm for more information.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
> As far as I know interfaces simply tell you whats available - if the
programmer didn't actually impliment the method correcly the interface won't
know.
What puzzled me is that, if the derived class didn't implement the abstract
method at all, then the method would be what C++ calls "pure abstract", and
I shouldn't have been allowed to even instantiate the derived class. What happens if you run this code, does it crash when calling Add?
No: it goes ahead and successfully adds something. Apparently, if you want
to prevent IList.Add from being used to Add an object of the wrong type to
your class derived from CollectionBase, then you can override OnInsert and
similar methods.
"ilPostino" <ne**@ip80.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl... Well Add isn't really instantiable via Derived (because of Abstract) and hence doesn't appear to be available however you cheated when trying to access it via the Interface. As far as I know interfaces simply tell you whats available - if the programmer didn't actually impliment the method correcly the interface won't know. What happens if you run this code, does it crash when calling Add? It will probably just do nothing as Add is in the base class and does nothing - it might be there but its probably empty.
C http://www.typemismatch.com/ (For Developers!) "Christopher Wells" <ch***************@invalid.invalid> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... There's something I don't understand about how the CollectionBase class
is defined. I can declare:
class Derived : CollectionBase { }
I can instantiate this Derived class:
Derived derived = new Derived();
I cannot call an Add method as I haven't declared it:
derived.Add(1); //compiler error: Derived does not contain a definition for Add
However CollectionBase derives from IList, IList declares Add as public abstract, and I can successfully call Add through the IList interface:
IList ilist = derived; ilist.Add(1);
So what is happening here:
* If neither CollectionBase nor Derived define Add, then why can I instantiate Derived, and call Add through the IList interface?
* Or if CollectionBase does implement Add, then why can't I see it in the Object browser and why do I get an error when I try to call it?
I understand how I'm *supposed* to use CollectionBase (I'm supposed to define an Add method in a derived class). What I don't understand is how CollectionBase itself is is declared or implemented: how is it that a Derived : CollectionBase can derive from IList, and be instantiable,
when the Add method isn't defined or implemented in Derived nor in CollectionBase, but is declared as abstract in IList?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Medi Montaseri |
last post by:
Hi,
Given a collection of similar but not exact entities (or products)
Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern
to provide a common interface to these products....
|
by: Vishal Gandhi |
last post by:
Hi ,
Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other...
|
by: Bjorn |
last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual
methods. If then someone wants to implement the interface they
needs to inherit from the class. If the implementing class...
|
by: Christopher Wells |
last post by:
There's something I don't understand about how the CollectionBase class is
defined. I can declare:
class Derived : CollectionBase
{
}
I can instantiate this Derived class:
Derived derived...
|
by: Dan Sikorsky |
last post by:
If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore 'abstract'
methods in a derived class, wouldn't that remove...
|
by: Rachel Devons |
last post by:
All,
I'm struggling with an OOP concept. Let me try to define what I'm wanting by
using some classic examples.
Let's say that I have classes called Square & Circle that derive from class...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
|
by: Jose Fernandez |
last post by:
Hello
I have been reading about the differences between virtual and abstract
keywords...
What i understand is that with VIRTUAL keyword, you can actually write the
whole method and override it...
|
by: emin.shopper |
last post by:
I had a need recently to check if my subclasses properly implemented
the desired interface and wished that I could use something like an
abstract base class in python. After reading up on metaclass...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |