Lack of support for explicit interface implementation in VB.Net 
Author Message
 Lack of support for explicit interface implementation in VB.Net

In translating Richard Week's C# code from  his book on Windows Forms Custom
Controls to VB.Net I have run into difficulty in a custom collection class.

1. Am I right in my assumption that only C# supports explicit interface
implementation?

2.  Is the VB workaround for creating a strongly typed collection class to
inherit from CollectionBase rather than the C# route of creating a
collection class that directly implements ICollection, IEnumerable and
IList?



Mon, 16 Aug 2004 03:19:15 GMT  
 Lack of support for explicit interface implementation in VB.Net
Not sure what you are getting at. You can implement an interface, as in:

Public Class MyCollection

  Implements ICollection

  Public ReadOnly Property Count() _
    As Integer _
    Implements System.Collections.ICollection.Count

    Get

    End Get

  End Property

  Public ReadOnly Property IsSynchronized()
    As Boolean _
    Implements System.Collections.ICollection.IsSynchronized

    Get

    End Get

  End Property

  Public ReadOnly Property SyncRoot() _
    As Object _
    Implements System.Collections.ICollection.SyncRoot

    Get

    End Get

  End Property

  Public Sub CopyTo(ByVal array As System.Array, _
      ByVal index As Integer) _
      Implements System.Collections.ICollection.CopyTo

  End Sub

  Public Function GetEnumerator() _
    As System.Collections.IEnumerator _
    Implements System.Collections.ICollection.GetEnumerator

  End Function

End Class
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
****
Think outside the box!
****************************************************************************
****

Quote:
> In translating Richard Week's C# code from  his book on Windows Forms
Custom
> Controls to VB.Net I have run into difficulty in a custom collection
class.

> 1. Am I right in my assumption that only C# supports explicit interface
> implementation?

> 2.  Is the VB workaround for creating a strongly typed collection class to
> inherit from CollectionBase rather than the C# route of creating a
> collection class that directly implements ICollection, IEnumerable and
> IList?



Mon, 16 Aug 2004 05:20:28 GMT  
 Lack of support for explicit interface implementation in VB.Net
Thanks for your reply.
It was in implementing some aspects of the IList interface that the author
used some C# tricks to make the collection strongly typed.
As an example in Implementing Ilist he has

int IList.AAA( object o)  {
    return this.Add( (specificClass)o);

Quote:
}

public int Add( specificClass spec) {
int idx = aninternalArrayList.Add(spec);
. . .
return idx;

Quote:
}

If an instace of the collection class is cast to IList, the Ilist
implementation of Add can be invoked
otherwise the strongly typed Add method is the only one available.

Can VB.Net mimic this trick?



Quote:
> Not sure what you are getting at. You can implement an interface, as in:

> Public Class MyCollection

>   Implements ICollection

>   Public ReadOnly Property Count() _
>     As Integer _
>     Implements System.Collections.ICollection.Count

>     Get

>     End Get

>   End Property

>   Public ReadOnly Property IsSynchronized()
>     As Boolean _
>     Implements System.Collections.ICollection.IsSynchronized

>     Get

>     End Get

>   End Property

>   Public ReadOnly Property SyncRoot() _
>     As Object _
>     Implements System.Collections.ICollection.SyncRoot

>     Get

>     End Get

>   End Property

>   Public Sub CopyTo(ByVal array As System.Array, _
>       ByVal index As Integer) _
>       Implements System.Collections.ICollection.CopyTo

>   End Sub

>   Public Function GetEnumerator() _
>     As System.Collections.IEnumerator _
>     Implements System.Collections.ICollection.GetEnumerator

>   End Function

> End Class
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
> Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> ****
> Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> ****


> > In translating Richard Week's C# code from  his book on Windows Forms
> Custom
> > Controls to VB.Net I have run into difficulty in a custom collection
> class.

> > 1. Am I right in my assumption that only C# supports explicit interface
> > implementation?

> > 2.  Is the VB workaround for creating a strongly typed collection class
to
> > inherit from CollectionBase rather than the C# route of creating a
> > collection class that directly implements ICollection, IEnumerable and
> > IList?



Mon, 16 Aug 2004 05:49:03 GMT  
 Lack of support for explicit interface implementation in VB.Net
Correction to first line of previous post
int IList.Add(object o) {

This is called "explicit interface implementation" by some authors.


Quote:
> Thanks for your reply.
> It was in implementing some aspects of the IList interface that the author
> used some C# tricks to make the collection strongly typed.
> As an example in Implementing Ilist he has

> int IList.AAA( object o)  {
>     return this.Add( (specificClass)o);
> }

> public int Add( specificClass spec) {
> int idx = aninternalArrayList.Add(spec);
> . . .
> return idx;
> }

> If an instace of the collection class is cast to IList, the Ilist
> implementation of Add can be invoked
> otherwise the strongly typed Add method is the only one available.

> Can VB.Net mimic this trick?



> > Not sure what you are getting at. You can implement an interface, as in:

> > Public Class MyCollection

> >   Implements ICollection

> >   Public ReadOnly Property Count() _
> >     As Integer _
> >     Implements System.Collections.ICollection.Count

> >     Get

> >     End Get

> >   End Property

> >   Public ReadOnly Property IsSynchronized()
> >     As Boolean _
> >     Implements System.Collections.ICollection.IsSynchronized

> >     Get

> >     End Get

> >   End Property

> >   Public ReadOnly Property SyncRoot() _
> >     As Object _
> >     Implements System.Collections.ICollection.SyncRoot

> >     Get

> >     End Get

> >   End Property

> >   Public Sub CopyTo(ByVal array As System.Array, _
> >       ByVal index As Integer) _
> >       Implements System.Collections.ICollection.CopyTo

> >   End Sub

> >   Public Function GetEnumerator() _
> >     As System.Collections.IEnumerator _
> >     Implements System.Collections.ICollection.GetEnumerator

> >   End Function

> > End Class
> > --
> > Gregory A. Beamer
> > MVP; MCP: +I, SE, SD, DBA
> > Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> > ****
> > Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> > ****


> > > In translating Richard Week's C# code from  his book on Windows Forms
> > Custom
> > > Controls to VB.Net I have run into difficulty in a custom collection
> > class.

> > > 1. Am I right in my assumption that only C# supports explicit
interface
> > > implementation?

> > > 2.  Is the VB workaround for creating a strongly typed collection
class
> to
> > > inherit from CollectionBase rather than the C# route of creating a
> > > collection class that directly implements ICollection, IEnumerable and
> > > IList?



Mon, 16 Aug 2004 06:02:02 GMT  
 Lack of support for explicit interface implementation in VB.Net
Michael,
Yep:

    Private Function Add(o as Object) As Integer Implements IList.Add
        Return Me.Add(CType(o, specificClass)
    End Function

    Public Function Add(spec as specificClass) As Integer
        ...
    End Function

Hope this helps
Jay


Quote:
> Thanks for your reply.
> It was in implementing some aspects of the IList interface that the author
> used some C# tricks to make the collection strongly typed.
> As an example in Implementing Ilist he has

> int IList.AAA( object o)  {
>     return this.Add( (specificClass)o);
> }

> public int Add( specificClass spec) {
> int idx = aninternalArrayList.Add(spec);
> . . .
> return idx;
> }

> If an instace of the collection class is cast to IList, the Ilist
> implementation of Add can be invoked
> otherwise the strongly typed Add method is the only one available.

> Can VB.Net mimic this trick?



> > Not sure what you are getting at. You can implement an interface, as in:

> > Public Class MyCollection

> >   Implements ICollection

> >   Public ReadOnly Property Count() _
> >     As Integer _
> >     Implements System.Collections.ICollection.Count

> >     Get

> >     End Get

> >   End Property

> >   Public ReadOnly Property IsSynchronized()
> >     As Boolean _
> >     Implements System.Collections.ICollection.IsSynchronized

> >     Get

> >     End Get

> >   End Property

> >   Public ReadOnly Property SyncRoot() _
> >     As Object _
> >     Implements System.Collections.ICollection.SyncRoot

> >     Get

> >     End Get

> >   End Property

> >   Public Sub CopyTo(ByVal array As System.Array, _
> >       ByVal index As Integer) _
> >       Implements System.Collections.ICollection.CopyTo

> >   End Sub

> >   Public Function GetEnumerator() _
> >     As System.Collections.IEnumerator _
> >     Implements System.Collections.ICollection.GetEnumerator

> >   End Function

> > End Class
> > --
> > Gregory A. Beamer
> > MVP; MCP: +I, SE, SD, DBA
> > Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> > ****
> > Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> > ****


> > > In translating Richard Week's C# code from  his book on Windows Forms
> > Custom
> > > Controls to VB.Net I have run into difficulty in a custom collection
> > class.

> > > 1. Am I right in my assumption that only C# supports explicit
interface
> > > implementation?

> > > 2.  Is the VB workaround for creating a strongly typed collection
class
> to
> > > inherit from CollectionBase rather than the C# route of creating a
> > > collection class that directly implements ICollection, IEnumerable and
> > > IList?



Mon, 16 Aug 2004 11:59:05 GMT  
 Lack of support for explicit interface implementation in VB.Net
HI Jay,

That's true for that [particular case, as the method signatures are different
due to the parameter.
However, when he get's to the Item property, he might run into trouble trying
to do the same <g>

The way I do this is a defacto naming rule similar to C#'s explicit interface
naming, except I use _ instead of .

So I would tend to write this as :

     Private Function IList_Add(o as Object) As Integer Implements IList.Add
         Return Me.Add(CType(o, specificClass)
     End Function

     Public Function Add(spec as specificClass) As Integer
         ...
     End Function

Oh, and Michael, if you are reading this, note how in VB.NET you can give the
method any naming you like as long as the signature matches the method being
implemented. The Implements keyword in VB.NET creates the interface mapping,
and doesn't have the limitations of C#'s interface implementation.  For
example, in VB.NET, if the interface was IAnimal and had an MakeNoise method,
you can map that to Dog:Bark, or Human:Talk without having to call an
intermediate procedure, simply by marking those procedures as Implements
IAnimal.MakeNoise.

But the fun doesn't stop there ! In VB.NET you can also easily map multiple
methods to the one method. Such as Sub Foo() Implements IFoo.Bar, IFoo2.Bar
etc



Quote:
> Michael,
> Yep:

>     Private Function Add(o as Object) As Integer Implements IList.Add
>         Return Me.Add(CType(o, specificClass)
>     End Function

>     Public Function Add(spec as specificClass) As Integer
>         ...
>     End Function

> Hope this helps
> Jay



> > Thanks for your reply.
> > It was in implementing some aspects of the IList interface that the author
> > used some C# tricks to make the collection strongly typed.
> > As an example in Implementing Ilist he has

> > int IList.AAA( object o)  {
> >     return this.Add( (specificClass)o);
> > }

> > public int Add( specificClass spec) {
> > int idx = aninternalArrayList.Add(spec);
> > . . .
> > return idx;
> > }

> > If an instace of the collection class is cast to IList, the Ilist
> > implementation of Add can be invoked
> > otherwise the strongly typed Add method is the only one available.

> > Can VB.Net mimic this trick?



> > > Not sure what you are getting at. You can implement an interface, as in:

> > > Public Class MyCollection

> > >   Implements ICollection

> > >   Public ReadOnly Property Count() _
> > >     As Integer _
> > >     Implements System.Collections.ICollection.Count

> > >     Get

> > >     End Get

> > >   End Property

> > >   Public ReadOnly Property IsSynchronized()
> > >     As Boolean _
> > >     Implements System.Collections.ICollection.IsSynchronized

> > >     Get

> > >     End Get

> > >   End Property

> > >   Public ReadOnly Property SyncRoot() _
> > >     As Object _
> > >     Implements System.Collections.ICollection.SyncRoot

> > >     Get

> > >     End Get

> > >   End Property

> > >   Public Sub CopyTo(ByVal array As System.Array, _
> > >       ByVal index As Integer) _
> > >       Implements System.Collections.ICollection.CopyTo

> > >   End Sub

> > >   Public Function GetEnumerator() _
> > >     As System.Collections.IEnumerator _
> > >     Implements System.Collections.ICollection.GetEnumerator

> > >   End Function

> > > End Class
> > > --
> > > Gregory A. Beamer
> > > MVP; MCP: +I, SE, SD, DBA
> > > Author: ADO.NET and XML: ASP.NET on the Edge

> ****************************************************************************
> > > ****
> > > Think outside the box!

> ****************************************************************************
> > > ****


> > > > In translating Richard Week's C# code from  his book on Windows Forms
> > > Custom
> > > > Controls to VB.Net I have run into difficulty in a custom collection
> > > class.

> > > > 1. Am I right in my assumption that only C# supports explicit
> interface
> > > > implementation?

> > > > 2.  Is the VB workaround for creating a strongly typed collection
> class
> > to
> > > > inherit from CollectionBase rather than the C# route of creating a
> > > > collection class that directly implements ICollection, IEnumerable and
> > > > IList?



Mon, 16 Aug 2004 20:16:56 GMT  
 Lack of support for explicit interface implementation in VB.Net

The advantage of the C# method is that the IList.Add method is hidden
whereas IList_Add is exposed to users although Bill rightly points out
several VB.Net advantages.

When cast to IList  type C# and VB.Net act the same.

Not enough here to force abandonment of my VB inclination since PDP11 days.

My thanks to all who replied.

Michael


Quote:
> HI Jay,

> That's true for that [particular case, as the method signatures are
different
> due to the parameter.
> However, when he get's to the Item property, he might run into trouble
trying
> to do the same <g>

> The way I do this is a defacto naming rule similar to C#'s explicit
interface
> naming, except I use _ instead of .

> So I would tend to write this as :

>      Private Function IList_Add(o as Object) As Integer Implements
IList.Add
>          Return Me.Add(CType(o, specificClass)
>      End Function

>      Public Function Add(spec as specificClass) As Integer
>          ...
>      End Function

> Oh, and Michael, if you are reading this, note how in VB.NET you can give
the
> method any naming you like as long as the signature matches the method
being
> implemented. The Implements keyword in VB.NET creates the interface
mapping,
> and doesn't have the limitations of C#'s interface implementation.  For
> example, in VB.NET, if the interface was IAnimal and had an MakeNoise
method,
> you can map that to Dog:Bark, or Human:Talk without having to call an
> intermediate procedure, simply by marking those procedures as Implements
> IAnimal.MakeNoise.

> But the fun doesn't stop there ! In VB.NET you can also easily map
multiple
> methods to the one method. Such as Sub Foo() Implements IFoo.Bar,
IFoo2.Bar
> etc


message

> > Michael,
> > Yep:

> >     Private Function Add(o as Object) As Integer Implements IList.Add
> >         Return Me.Add(CType(o, specificClass)
> >     End Function

> >     Public Function Add(spec as specificClass) As Integer
> >         ...
> >     End Function

> > Hope this helps
> > Jay



> > > Thanks for your reply.
> > > It was in implementing some aspects of the IList interface that the
author
> > > used some C# tricks to make the collection strongly typed.
> > > As an example in Implementing Ilist he has

> > > int IList.AAA( object o)  {
> > >     return this.Add( (specificClass)o);
> > > }

> > > public int Add( specificClass spec) {
> > > int idx = aninternalArrayList.Add(spec);
> > > . . .
> > > return idx;
> > > }

> > > If an instace of the collection class is cast to IList, the Ilist
> > > implementation of Add can be invoked
> > > otherwise the strongly typed Add method is the only one available.

> > > Can VB.Net mimic this trick?

> > > "Cowboy (aka Gregory A. Beamer) [MVP]"


- Show quoted text -

Quote:

> > > > Not sure what you are getting at. You can implement an interface, as
in:

> > > > Public Class MyCollection

> > > >   Implements ICollection

> > > >   Public ReadOnly Property Count() _
> > > >     As Integer _
> > > >     Implements System.Collections.ICollection.Count

> > > >     Get

> > > >     End Get

> > > >   End Property

> > > >   Public ReadOnly Property IsSynchronized()
> > > >     As Boolean _
> > > >     Implements System.Collections.ICollection.IsSynchronized

> > > >     Get

> > > >     End Get

> > > >   End Property

> > > >   Public ReadOnly Property SyncRoot() _
> > > >     As Object _
> > > >     Implements System.Collections.ICollection.SyncRoot

> > > >     Get

> > > >     End Get

> > > >   End Property

> > > >   Public Sub CopyTo(ByVal array As System.Array, _
> > > >       ByVal index As Integer) _
> > > >       Implements System.Collections.ICollection.CopyTo

> > > >   End Sub

> > > >   Public Function GetEnumerator() _
> > > >     As System.Collections.IEnumerator _
> > > >     Implements System.Collections.ICollection.GetEnumerator

> > > >   End Function

> > > > End Class
> > > > --
> > > > Gregory A. Beamer
> > > > MVP; MCP: +I, SE, SD, DBA
> > > > Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> > > > ****
> > > > Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> > > > ****


> > > > > In translating Richard Week's C# code from  his book on Windows
Forms
> > > > Custom
> > > > > Controls to VB.Net I have run into difficulty in a custom
collection
> > > > class.

> > > > > 1. Am I right in my assumption that only C# supports explicit
> > interface
> > > > > implementation?

> > > > > 2.  Is the VB workaround for creating a strongly typed collection
> > class
> > > to
> > > > > inherit from CollectionBase rather than the C# route of creating a
> > > > > collection class that directly implements ICollection, IEnumerable
and
> > > > > IList?



Tue, 17 Aug 2004 01:43:02 GMT  
 Lack of support for explicit interface implementation in VB.Net
Michael,
NOTE: In both Bill's and my example, the IList.Add method was marked
PRIVATE!

Only the class itself can see the method, users will not be able to see it.

Hope this helps
Jay


Quote:

> The advantage of the C# method is that the IList.Add method is hidden
> whereas IList_Add is exposed to users although Bill rightly points out
> several VB.Net advantages.

> When cast to IList  type C# and VB.Net act the same.

> Not enough here to force abandonment of my VB inclination since PDP11
days.

> My thanks to all who replied.

> Michael



> > HI Jay,

> > That's true for that [particular case, as the method signatures are
> different
> > due to the parameter.
> > However, when he get's to the Item property, he might run into trouble
> trying
> > to do the same <g>

> > The way I do this is a defacto naming rule similar to C#'s explicit
> interface
> > naming, except I use _ instead of .

> > So I would tend to write this as :

> >      Private Function IList_Add(o as Object) As Integer Implements
> IList.Add
> >          Return Me.Add(CType(o, specificClass)
> >      End Function

> >      Public Function Add(spec as specificClass) As Integer
> >          ...
> >      End Function

> > Oh, and Michael, if you are reading this, note how in VB.NET you can
give
> the
> > method any naming you like as long as the signature matches the method
> being
> > implemented. The Implements keyword in VB.NET creates the interface
> mapping,
> > and doesn't have the limitations of C#'s interface implementation.  For
> > example, in VB.NET, if the interface was IAnimal and had an MakeNoise
> method,
> > you can map that to Dog:Bark, or Human:Talk without having to call an
> > intermediate procedure, simply by marking those procedures as Implements
> > IAnimal.MakeNoise.

> > But the fun doesn't stop there ! In VB.NET you can also easily map
> multiple
> > methods to the one method. Such as Sub Foo() Implements IFoo.Bar,
> IFoo2.Bar
> > etc


> message

> > > Michael,
> > > Yep:

> > >     Private Function Add(o as Object) As Integer Implements IList.Add
> > >         Return Me.Add(CType(o, specificClass)
> > >     End Function

> > >     Public Function Add(spec as specificClass) As Integer
> > >         ...
> > >     End Function

> > > Hope this helps
> > > Jay



> > > > Thanks for your reply.
> > > > It was in implementing some aspects of the IList interface that the
> author
> > > > used some C# tricks to make the collection strongly typed.
> > > > As an example in Implementing Ilist he has

> > > > int IList.AAA( object o)  {
> > > >     return this.Add( (specificClass)o);
> > > > }

> > > > public int Add( specificClass spec) {
> > > > int idx = aninternalArrayList.Add(spec);
> > > > . . .
> > > > return idx;
> > > > }

> > > > If an instace of the collection class is cast to IList, the Ilist
> > > > implementation of Add can be invoked
> > > > otherwise the strongly typed Add method is the only one available.

> > > > Can VB.Net mimic this trick?

> > > > "Cowboy (aka Gregory A. Beamer) [MVP]"


> > > > > Not sure what you are getting at. You can implement an interface,
as
> in:

> > > > > Public Class MyCollection

> > > > >   Implements ICollection

> > > > >   Public ReadOnly Property Count() _
> > > > >     As Integer _
> > > > >     Implements System.Collections.ICollection.Count

> > > > >     Get

> > > > >     End Get

> > > > >   End Property

> > > > >   Public ReadOnly Property IsSynchronized()
> > > > >     As Boolean _
> > > > >     Implements System.Collections.ICollection.IsSynchronized

> > > > >     Get

> > > > >     End Get

> > > > >   End Property

> > > > >   Public ReadOnly Property SyncRoot() _
> > > > >     As Object _
> > > > >     Implements System.Collections.ICollection.SyncRoot

> > > > >     Get

> > > > >     End Get

> > > > >   End Property

> > > > >   Public Sub CopyTo(ByVal array As System.Array, _
> > > > >       ByVal index As Integer) _
> > > > >       Implements System.Collections.ICollection.CopyTo

> > > > >   End Sub

> > > > >   Public Function GetEnumerator() _
> > > > >     As System.Collections.IEnumerator _
> > > > >     Implements System.Collections.ICollection.GetEnumerator

> > > > >   End Function

> > > > > End Class
> > > > > --
> > > > > Gregory A. Beamer
> > > > > MVP; MCP: +I, SE, SD, DBA
> > > > > Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> > > > > ****
> > > > > Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> > > > > ****


> > > > > > In translating Richard Week's C# code from  his book on Windows
> Forms
> > > > > Custom
> > > > > > Controls to VB.Net I have run into difficulty in a custom
> collection
> > > > > class.

> > > > > > 1. Am I right in my assumption that only C# supports explicit
> > > interface
> > > > > > implementation?

> > > > > > 2.  Is the VB workaround for creating a strongly typed
collection
> > > class
> > > > to
> > > > > > inherit from CollectionBase rather than the C# route of creating
a
> > > > > > collection class that directly implements ICollection,
IEnumerable
> and
> > > > > > IList?



Tue, 17 Aug 2004 10:02:02 GMT  
 Lack of support for explicit interface implementation in VB.Net

Quote:
> The advantage of the C# method is that the IList.Add method is hidden
> whereas IList_Add is exposed to users

It is only exposed if you mark it public. In Bill's example it was marked
private, and thus is only available via the IList.

--
Jonathan Allen


Quote:

> The advantage of the C# method is that the IList.Add method is hidden
> whereas IList_Add is exposed to users although Bill rightly points out
> several VB.Net advantages.

> When cast to IList  type C# and VB.Net act the same.

> Not enough here to force abandonment of my VB inclination since PDP11
days.

> My thanks to all who replied.

> Michael



> > HI Jay,

> > That's true for that [particular case, as the method signatures are
> different
> > due to the parameter.
> > However, when he get's to the Item property, he might run into trouble
> trying
> > to do the same <g>

> > The way I do this is a defacto naming rule similar to C#'s explicit
> interface
> > naming, except I use _ instead of .

> > So I would tend to write this as :

> >      Private Function IList_Add(o as Object) As Integer Implements
> IList.Add
> >          Return Me.Add(CType(o, specificClass)
> >      End Function

> >      Public Function Add(spec as specificClass) As Integer
> >          ...
> >      End Function

> > Oh, and Michael, if you are reading this, note how in VB.NET you can
give
> the
> > method any naming you like as long as the signature matches the method
> being
> > implemented. The Implements keyword in VB.NET creates the interface
> mapping,
> > and doesn't have the limitations of C#'s interface implementation.  For
> > example, in VB.NET, if the interface was IAnimal and had an MakeNoise
> method,
> > you can map that to Dog:Bark, or Human:Talk without having to call an
> > intermediate procedure, simply by marking those procedures as Implements
> > IAnimal.MakeNoise.

> > But the fun doesn't stop there ! In VB.NET you can also easily map
> multiple
> > methods to the one method. Such as Sub Foo() Implements IFoo.Bar,
> IFoo2.Bar
> > etc



Tue, 17 Aug 2004 09:17:33 GMT  
 Lack of support for explicit interface implementation in VB.Net
Mea culpa, I didn't spot the Private access modifier! Perhaps because C#
actually disallows access modifiers in this case.
I also did not realise that a user using an interface variable can invoke
this private implementation through the public interface.

Many thanks to all of you

Michael



Quote:
> Michael,
> NOTE: In both Bill's and my example, the IList.Add method was marked
> PRIVATE!

> Only the class itself can see the method, users will not be able to see
it.

> Hope this helps
> Jay



> > The advantage of the C# method is that the IList.Add method is hidden
> > whereas IList_Add is exposed to users although Bill rightly points out
> > several VB.Net advantages.

> > When cast to IList  type C# and VB.Net act the same.

> > Not enough here to force abandonment of my VB inclination since PDP11
> days.

> > My thanks to all who replied.

> > Michael



> > > HI Jay,

> > > That's true for that [particular case, as the method signatures are
> > different
> > > due to the parameter.
> > > However, when he get's to the Item property, he might run into trouble
> > trying
> > > to do the same <g>

> > > The way I do this is a defacto naming rule similar to C#'s explicit
> > interface
> > > naming, except I use _ instead of .

> > > So I would tend to write this as :

> > >      Private Function IList_Add(o as Object) As Integer Implements
> > IList.Add
> > >          Return Me.Add(CType(o, specificClass)
> > >      End Function

> > >      Public Function Add(spec as specificClass) As Integer
> > >          ...
> > >      End Function

> > > Oh, and Michael, if you are reading this, note how in VB.NET you can
> give
> > the
> > > method any naming you like as long as the signature matches the method
> > being
> > > implemented. The Implements keyword in VB.NET creates the interface
> > mapping,
> > > and doesn't have the limitations of C#'s interface implementation.
For
> > > example, in VB.NET, if the interface was IAnimal and had an MakeNoise
> > method,
> > > you can map that to Dog:Bark, or Human:Talk without having to call an
> > > intermediate procedure, simply by marking those procedures as
Implements
> > > IAnimal.MakeNoise.

> > > But the fun doesn't stop there ! In VB.NET you can also easily map
> > multiple
> > > methods to the one method. Such as Sub Foo() Implements IFoo.Bar,
> > IFoo2.Bar
> > > etc


> > message

> > > > Michael,
> > > > Yep:

> > > >     Private Function Add(o as Object) As Integer Implements
IList.Add
> > > >         Return Me.Add(CType(o, specificClass)
> > > >     End Function

> > > >     Public Function Add(spec as specificClass) As Integer
> > > >         ...
> > > >     End Function

> > > > Hope this helps
> > > > Jay



> > > > > Thanks for your reply.
> > > > > It was in implementing some aspects of the IList interface that
the
> > author
> > > > > used some C# tricks to make the collection strongly typed.
> > > > > As an example in Implementing Ilist he has

> > > > > int IList.AAA( object o)  {
> > > > >     return this.Add( (specificClass)o);
> > > > > }

> > > > > public int Add( specificClass spec) {
> > > > > int idx = aninternalArrayList.Add(spec);
> > > > > . . .
> > > > > return idx;
> > > > > }

> > > > > If an instace of the collection class is cast to IList, the Ilist
> > > > > implementation of Add can be invoked
> > > > > otherwise the strongly typed Add method is the only one available.

> > > > > Can VB.Net mimic this trick?

> > > > > "Cowboy (aka Gregory A. Beamer) [MVP]"


> > > > > > Not sure what you are getting at. You can implement an
interface,
> as
> > in:

> > > > > > Public Class MyCollection

> > > > > >   Implements ICollection

> > > > > >   Public ReadOnly Property Count() _
> > > > > >     As Integer _
> > > > > >     Implements System.Collections.ICollection.Count

> > > > > >     Get

> > > > > >     End Get

> > > > > >   End Property

> > > > > >   Public ReadOnly Property IsSynchronized()
> > > > > >     As Boolean _
> > > > > >     Implements System.Collections.ICollection.IsSynchronized

> > > > > >     Get

> > > > > >     End Get

> > > > > >   End Property

> > > > > >   Public ReadOnly Property SyncRoot() _
> > > > > >     As Object _
> > > > > >     Implements System.Collections.ICollection.SyncRoot

> > > > > >     Get

> > > > > >     End Get

> > > > > >   End Property

> > > > > >   Public Sub CopyTo(ByVal array As System.Array, _
> > > > > >       ByVal index As Integer) _
> > > > > >       Implements System.Collections.ICollection.CopyTo

> > > > > >   End Sub

> > > > > >   Public Function GetEnumerator() _
> > > > > >     As System.Collections.IEnumerator _
> > > > > >     Implements System.Collections.ICollection.GetEnumerator

> > > > > >   End Function

> > > > > > End Class
> > > > > > --
> > > > > > Gregory A. Beamer
> > > > > > MVP; MCP: +I, SE, SD, DBA
> > > > > > Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
Quote:
> > > > > > ****
> > > > > > Think outside the box!

****************************************************************************

- Show quoted text -

Quote:
> > > > > > ****


> > > > > > > In translating Richard Week's C# code from  his book on
Windows
> > Forms
> > > > > > Custom
> > > > > > > Controls to VB.Net I have run into difficulty in a custom
> > collection
> > > > > > class.

> > > > > > > 1. Am I right in my assumption that only C# supports explicit
> > > > interface
> > > > > > > implementation?

> > > > > > > 2.  Is the VB workaround for creating a strongly typed
> collection
> > > > class
> > > > > to
> > > > > > > inherit from CollectionBase rather than the C# route of
creating
> a
> > > > > > > collection class that directly implements ICollection,
> IEnumerable
> > and
> > > > > > > IList?



Tue, 17 Aug 2004 23:22:48 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Explicit interface implementation?

2. virtual events, explicit interface implementation

3. VB Interface Implementation - Novice Question

4. VB COM Interface Implementation

5. Interface implementation in VB => nod Debugging

6. ASP pages, slow, lack of debugging support - SOLUTION

7. MDAC 2.6 Lack of support for Jet, etc...

8. Code: Streaming RC4 implementation in VB.NET

9. Anonymous Pipe Implementation using VB.Net ?

10. Code: Streaming RC4 implementation in VB.NET

11. CLASS DOES NOT SUPPORT AUTOMATION O DON′T SUPPORT EXPECTED INTERFACE

12. Error 430 : Class does not support Automation or Does not support expected Interface

 

 
Powered by phpBB® Forum Software