473,387 Members | 1,859 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.

Interfaces

So I'm trying to make a generic TreeNode interface in order to simplify converting between a Windows Forms TreeView and an ASP.NET TreeView. I define one of the properties on the interface (Children) as an ICollection, but when I then try to implement the property as a Collection (or ArrayList, or any other class that implements ICollection), it doesn't work. The same is true if I define the interface property as CollectionBase. Am I doing something wrong, or is this just a VB limitation, or something else? I just don't see the point of creating an interface if it can't be defined using base classes or other interfaces... that really limits the usefulness of interfaces as far as I can tell. Any suggestions would be greatly appreciate. Thanks.

Mike
Nov 20 '05 #1
2 1714
Mike,
Can you provide code for what you are trying?

Remember that if you define the method in the Interface with "ICollection"
the implementing method also needs to be ICollection! However you can use
"Explicit Interface Implementation" where you can "overload" the return type
of the implementing method.

Something like:

Public Interface ITreeNode
ReadOnly Property Children() As ICollection
End Interface

Public Class TreeNode
Implements ITreeNode

Private ReadOnly m_children As TreeNodeCollection

Private ReadOnly Property ITreeNode_Children() As
System.Collections.ICollection Implements ITreeNode.Children
Get
Return Me.Children
End Get
End Property

Private ReadOnly Property Children() As TreeNodeCollection
Get
Return m_children
End Get
End Property

End Class

The TreeNode.Children is the type safe collection that matches TreeNode,
while ITreeNode_Children is the version of the method required by the
Interface.

Hope this helps
Jay

"Mike Caputo" <Mi********@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
So I'm trying to make a generic TreeNode interface in order to simplify converting between a Windows Forms TreeView and an ASP.NET TreeView. I
define one of the properties on the interface (Children) as an ICollection,
but when I then try to implement the property as a Collection (or ArrayList,
or any other class that implements ICollection), it doesn't work. The same
is true if I define the interface property as CollectionBase. Am I doing
something wrong, or is this just a VB limitation, or something else? I just
don't see the point of creating an interface if it can't be defined using
base classes or other interfaces... that really limits the usefulness of
interfaces as far as I can tell. Any suggestions would be greatly
appreciate. Thanks.
Mike

Nov 20 '05 #2
Mike,
I still think the use of derived classes and/or classes
which implement the interface defined on the property
in the interface being implemented Covariant return types are not supported by VB.NET as they are not supported
by the CLR.

Covariant return types is the ability for a method in a derived class to
return a type that is derived from the return type of the base class's
method, given the rest of the signature is identical.

Eiffel .NET is able to support them within Eiffel code.

There are also Contravariant/covariant parameters, which VB.NET & the CLR
are also not able to support.
should be available directly, but I think this will work for now. What I showed currently is the only way to do it...

Hope this helps
Jay

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:DC**********************************@microsof t.com... Hey thanks a lot Jay, sounds like that will solve my problem. I didn't realize the property could be implemented with a different access scope
(i.e. Private) from the one defined in the interface. I still think the use
of derived classes and/or classes which implement the interface defined on
the property in the interface being implemented (that was a very confusing
phrase, but I think you know I'm saying) should be available directly, but I
think this will work for now. Thanks again!
Mike

"Jay B. Harlow [MVP - Outlook]" wrote:
Mike,
Can you provide code for what you are trying?

Remember that if you define the method in the Interface with "ICollection" the implementing method also needs to be ICollection! However you can use "Explicit Interface Implementation" where you can "overload" the return type of the implementing method.

Something like:

Public Interface ITreeNode
ReadOnly Property Children() As ICollection
End Interface

Public Class TreeNode
Implements ITreeNode

Private ReadOnly m_children As TreeNodeCollection

Private ReadOnly Property ITreeNode_Children() As
System.Collections.ICollection Implements ITreeNode.Children
Get
Return Me.Children
End Get
End Property

Private ReadOnly Property Children() As TreeNodeCollection
Get
Return m_children
End Get
End Property

End Class

The TreeNode.Children is the type safe collection that matches TreeNode,
while ITreeNode_Children is the version of the method required by the
Interface.

Hope this helps
Jay

"Mike Caputo" <Mi********@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
So I'm trying to make a generic TreeNode interface in order to
simplify converting between a Windows Forms TreeView and an ASP.NET TreeView. I
define one of the properties on the interface (Children) as an ICollection, but when I then try to implement the property as a Collection (or ArrayList, or any other class that implements ICollection), it doesn't work. The same is true if I define the interface property as CollectionBase. Am I doing something wrong, or is this just a VB limitation, or something else? I just don't see the point of creating an interface if it can't be defined using base classes or other interfaces... that really limits the usefulness of
interfaces as far as I can tell. Any suggestions would be greatly
appreciate. Thanks.

Mike


Nov 20 '05 #3

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

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
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
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
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...
10
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
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,...
0
jinu1996
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...

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.