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 2787
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!)
"Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.inv alid> 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
ilPostino <ne**@ip80.co m> 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.co m> 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!
"Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in messag
news:%2******** ********@TK2MSF TNGP09.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Christopher Wells <ch************ ***@invalid.inv alid> 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.co m> 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.co m> wrote in message
news:uE******** ******@TK2MSFTN GP12.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!) "Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in message news:%2******** ********@TK2MSF TNGP09.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. So I shall have an
Abstract Base called 'Car' implemented by Toyota, Ford, and Buick.
Further I'd like to enable to client to say
Car *factory;
|
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 then instantiation ?
Many Thanks
Vishal Gandhi
|
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 forgets to
implement some method I normally get a compile error(if the class is created
in some way of course). When using the Visual
Studio 6 compiler however, it does not generate a compile error in this
case:
- I have a implementing class A...
|
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 = new 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 the need to have abstract
class types in C#?
Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition
for the abstract method when defining the containing...
| |
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
Shape. Within class Shape, it has a method called Save to save the object to
disk. It's defined in Shape because the logic to save the shapes is uniform
across all shapes.
Furthermore, let's say I've defined an interface called IDraw that includes
|
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 looked for one on the net (I did browse the
boost library though). It doesn't matter right now, anyway.
To put it simply, Abstract Iterator is mainly a wrapper class. It helps
|
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 if needed... with abstract, you just declare
the method name and must write the code in the inherited class. Am I right??
public class House{
|
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 magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g., forgetting
to implement a method required by the given interface). This is handy
when unit tests or...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |