473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Delegates, abstract classes and interfaces

Hi

Consider the following code snipet:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch() {
ArrayList al = new ArrayList();
al.Add("hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(al);
}

void SomeMethod(ArrayList al) {
// do something
Console.Write(al.Count.ToString());
}
I get a compile-time error telling me that I'm not using signature that
matches the delegate. I'm not sure why the runtime doesn't support this, can
anyone explain this one to me. I can sub out an interface for a abstract
class with the same effect.

Aug 21 '06 #1
4 2676
Craig wrote:
Hi

Consider the following code snipet:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch() {
ArrayList al = new ArrayList();
al.Add("hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(al);
}

void SomeMethod(ArrayList al) {
// do something
Console.Write(al.Count.ToString());
}
I get a compile-time error telling me that I'm not using signature
that matches the delegate. I'm not sure why the runtime doesn't
support this, can anyone explain this one to me. I can sub out an
interface for a abstract class with the same effect.
change
void SomeMethod(ArrayList al) {
into
void SomeMethod(ICollection collection) {

As the delegate method signature you defined contains ICollection as
the type of the parameter, though the method you specified has
ArrayList as the type. This could lead to runtime errors when you pass
an ICollection implementing type which isn't an ArrayList to the
delegate and which then thus creates a runtime exception, something you
don't want to happen, so the compiler checked this for you up front.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Aug 21 '06 #2
Hi Frans

Thanks for the reply. I realise if I change the signature of the delegate
to match the function all would be ok.

Here's a snipet of what I want to do:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch(ICollection collection) {
SomeDelegate sd = null;
if(collection is ArrayList) {
sd = new SomeDelegate(SomeMethod);
}
else if (collection is BitAray) {
sd = new SomeDelegate(SomeMethod1);
}

sd.Invoke(collection);
}

void SomeMethod(ArrayList al) {
// do something specific for an ArrayList
Console.Write(al.Count.ToString());
}

void SomeMethod1(BitArray ba) {
// do something specific to a BitArray
Console.Write(ba.Count.ToString());
}

Whether I use an interface or an abstract class in the delegate signature I
fully expected the compiler to reflect the actual passed argument ensuring it
was the correct or derived from the correct type.

Do you think is a reasonable assumption?

Craig
"Frans Bouma [C# MVP]" wrote:
Craig wrote:
Hi

Consider the following code snipet:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch() {
ArrayList al = new ArrayList();
al.Add("hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(al);
}

void SomeMethod(ArrayList al) {
// do something
Console.Write(al.Count.ToString());
}
I get a compile-time error telling me that I'm not using signature
that matches the delegate. I'm not sure why the runtime doesn't
support this, can anyone explain this one to me. I can sub out an
interface for a abstract class with the same effect.

change
void SomeMethod(ArrayList al) {
into
void SomeMethod(ICollection collection) {

As the delegate method signature you defined contains ICollection as
the type of the parameter, though the method you specified has
ArrayList as the type. This could lead to runtime errors when you pass
an ICollection implementing type which isn't an ArrayList to the
delegate and which then thus creates a runtime exception, something you
don't want to happen, so the compiler checked this for you up front.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Aug 21 '06 #3
Craig wrote:
Hi Frans

Thanks for the reply. I realise if I change the signature of the delegate
to match the function all would be ok.

Here's a snipet of what I want to do:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch(ICollection collection) {
SomeDelegate sd = null;
if(collection is ArrayList) {
sd = new SomeDelegate(SomeMethod);
}
else if (collection is BitAray) {
sd = new SomeDelegate(SomeMethod1);
}

sd.Invoke(collection);
}

void SomeMethod(ArrayList al) {
// do something specific for an ArrayList
Console.Write(al.Count.ToString());
}

void SomeMethod1(BitArray ba) {
// do something specific to a BitArray
Console.Write(ba.Count.ToString());
}
As you need to query the specific type in your dispatch method anyway,
I would do the needed downcast right there:

namespace ConsoleApplication
{
delegate void SomeDelegateArrayList(ArrayList collection);
delegate void SomeDelegateSortedList(SortedList collection);
class Program
{
static void SomeDispatch(ICollection collection)
{
Delegate sd = new SomeDelegateArrayList(SomeMethodDefault);
if (collection is ArrayList)
{
sd = new SomeDelegateArrayList(SomeMethodArrayList);
}
else if (collection is SortedList)
{
sd = new SomeDelegateSortedList(SomeMethodSortedList);
}

sd.DynamicInvoke(collection);
}
static void SomeMethodArrayList(ArrayList al)
{
// do something
Console.Write(al.Count.ToString());
}
static void SomeMethodSortedList(SortedList sl)
{
// do something
Console.Write(sl.Count.ToString());
}
static void SomeMethodDefault(ICollection al)
{
// do sensible default??
throw new NotImplementedException();
}
static void Main(string[] args)
{
}
}
}

>
Whether I use an interface or an abstract class in the delegate signature I
fully expected the compiler to reflect the actual passed argument ensuring it
was the correct or derived from the correct type.

Do you think is a reasonable assumption?
Nope. If this would be possible you could do something like this:

namespace ConsoleApplication
{
delegate void SomeDelegate(ICollection collection);
class Program
{
static void SomeDispatch()
{
SortedList sl = new SortedList();
sl.Add("hello", "hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(sl);
}

static void SomeMethod(ArrayList al)
{
// do something
al.AddRange(new int[]{1, 2});// How would that work for a
// sorted list??
}
static void Main(string[] args)
{
}
}
}

The delegate is a contract. It says: "When you have a delegate of this
type, it can work with an ICollection."

Your function has a contract stating: "This function can handle an
ArrayList"

ArrayList is a ICollection, but you can do a lot more with it! So how
should the compiler and/or runtime handle it, especially if all these
bits are defined across different assemblies?

BTW, in classic C++ or Java you would model a delegate using an interface:

interface ISomeDelegate
{
void SomeMethod(ICollection al);
}

and what you expect would look like this:

class SomeDelegate : ISomeDelegate
{
void SomeMethod(ICollection al);
}

which wouldn't compile either.
BTW, it does work the other way round:

namespace ConsoleApplication
{
// delegate taking the specialized type
delegate void SomeDelegate(ArrayList collection);
class Program
{
static void SomeDispatch()
{
ArrayList al = new ArrayList();
al.Add("hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(al);
}
// function that takes the base type
static void SomeMethod(ICollection al)
{
// do something
Console.Write(al.Count.ToString());
}
static void Main(string[] args)
{
}
}
}

HTH,
Andy
Aug 21 '06 #4
Hi Craig,

That is because not all ICollection implementations are ArrayList objects.

Consider invoking sd with a SortedList. The delegate signature will support the call, however the concrete method will not.

--
Dave Sexton

"Craig" <Cr***@discussions.microsoft.comwrote in message news:29**********************************@microsof t.com...
Hi

Consider the following code snipet:

delegate void SomeDelegate(ICollection collection);

void SomeDispatch() {
ArrayList al = new ArrayList();
al.Add("hello");

SomeDelegate sd = new SomeDelegate(SomeMethod);
sd.Invoke(al);
}

void SomeMethod(ArrayList al) {
// do something
Console.Write(al.Count.ToString());
}
I get a compile-time error telling me that I'm not using signature that
matches the delegate. I'm not sure why the runtime doesn't support this, can
anyone explain this one to me. I can sub out an interface for a abstract
class with the same effect.

Aug 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
2
by: Berryl Hesh | last post by:
I'm wondering if a delegate will help me solve how to reuse the unit test fixture below (I only show one test of several here). The parts that I want to vary in it are the IClosingWindowView &...
4
by: samadams_2006 | last post by:
Hello, I'm trying to figure out why delegates are such a great thing. For example, I came across the following line of code: "This ability to refer to a method as a parameter makes delegates...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.