Reimplementing interfaces in vb


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Reimplementing interfaces in vb

  1. #1
    Ed Pinto Guest

    Reimplementing interfaces in vb


    I've been trying to reimplement an interface in vb, but the compiler won't
    let me.

    Class Base implements ITest. Class Derived inherits from Class Base and
    tries to implement ITest, but can't.

    I'd like to know if this is because I've got improper scoping or visibility
    issues or if it simply is not possible in vb.

    I'm puzzled because C# let's me do this without a problem. And this is so
    fundamental that I'd be really surprised if this feature was not in both
    languages.

  2. #2
    Rob Teixeira Guest

    Re: Reimplementing interfaces in vb


    I haven't been able to find a way to reimplement an interface in from a derived
    class VB.NET (beta 2) either.

    Of course, you do have several options such as using interface implementation
    throughout instead of inheritance and reimplementation (yuck... in some cases).
    However, I did find that if you make the implementing member friend or public
    in the base class, you can override it in the derived class, and that override
    will be used as the implementation for the interface in the derived class.

    In other words:
    Option Strict On

    Public Interface ITest
    Function Foo() As Integer
    End Interface

    Public Class TestBase
    Implements ITest

    Public Function Bar() As Integer
    Return 100
    End Function

    Friend Overridable Function Foo() As Integer Implements ITest.Foo
    Return 1
    End Function
    End Class

    Public Class TestDerive
    Inherits TestBase

    Friend Overrides Function Foo() As Integer
    Return 2
    End Function
    End Class


    I haven't thought too much about this while working with VB.NET so far, so
    I'll have to dig into the docs a bit more to see how it's supposed to work,
    and why there is a limitation in reimplementation of a common interface on
    inherited classes.

    -Rob


    "Ed Pinto" <ed.pinto@eds.com> wrote:
    >
    >I've been trying to reimplement an interface in vb, but the compiler won't
    >let me.
    >
    >Class Base implements ITest. Class Derived inherits from Class Base and
    >tries to implement ITest, but can't.
    >
    >I'd like to know if this is because I've got improper scoping or visibility
    >issues or if it simply is not possible in vb.
    >
    >I'm puzzled because C# let's me do this without a problem. And this is

    so
    >fundamental that I'd be really surprised if this feature was not in both
    >languages.



  3. #3
    Ed Pinto Guest

    Re: Reimplementing interfaces in vb



    Thanks Rob. I've been using that technique where appropriate. I guess the
    big problem I'm facing is that I don't have control over the all my base
    classes, some of them are SDK. Microsoft does. I can't override their implementations
    because the scope isn't right.... C# looks like my only option.

    >I haven't been able to find a way to reimplement an interface in from a

    derived
    >class VB.NET (beta 2) either.
    >
    >Of course, you do have several options such as using interface implementation
    >throughout instead of inheritance and reimplementation (yuck... in some

    cases).
    >However, I did find that if you make the implementing member friend or public
    >in the base class, you can override it in the derived class, and that override
    >will be used as the implementation for the interface in the derived class.
    >
    >In other words:
    >Option Strict On
    >
    >Public Interface ITest
    > Function Foo() As Integer
    >End Interface
    >
    >Public Class TestBase
    > Implements ITest
    >
    > Public Function Bar() As Integer
    > Return 100
    > End Function
    >
    > Friend Overridable Function Foo() As Integer Implements ITest.Foo
    > Return 1
    > End Function
    >End Class
    >
    >Public Class TestDerive
    > Inherits TestBase
    >
    > Friend Overrides Function Foo() As Integer
    > Return 2
    > End Function
    >End Class
    >
    >
    >I haven't thought too much about this while working with VB.NET so far,

    so
    >I'll have to dig into the docs a bit more to see how it's supposed to work,
    >and why there is a limitation in reimplementation of a common interface

    on
    >inherited classes.
    >
    >-Rob
    >
    >
    >"Ed Pinto" <ed.pinto@eds.com> wrote:
    >>
    >>I've been trying to reimplement an interface in vb, but the compiler won't
    >>let me.
    >>
    >>Class Base implements ITest. Class Derived inherits from Class Base and
    >>tries to implement ITest, but can't.
    >>
    >>I'd like to know if this is because I've got improper scoping or visibility
    >>issues or if it simply is not possible in vb.
    >>
    >>I'm puzzled because C# let's me do this without a problem. And this is

    >so
    >>fundamental that I'd be really surprised if this feature was not in both
    >>languages.

    >



  4. #4
    Rob Teixeira Guest

    Re: Reimplementing interfaces in vb


    You can also look into Shadowing, although I'm not sure how useful it will
    be in the context of implementing an interface.

    I'm hoping one of the MS guys will pop in and tell me I'm wrong and there
    is a way to do what we're talking about. Either that or tell me why we can't


    -Rob

    "Ed Pinto" <ed.pinto@eds.com> wrote:
    >
    >
    >Thanks Rob. I've been using that technique where appropriate. I guess

    the
    >big problem I'm facing is that I don't have control over the all my base
    >classes, some of them are SDK. Microsoft does. I can't override their

    implementations
    >because the scope isn't right.... C# looks like my only option.
    >
    >>I haven't been able to find a way to reimplement an interface in from a

    >derived
    >>class VB.NET (beta 2) either.
    >>
    >>Of course, you do have several options such as using interface implementation
    >>throughout instead of inheritance and reimplementation (yuck... in some

    >cases).
    >>However, I did find that if you make the implementing member friend or

    public
    >>in the base class, you can override it in the derived class, and that override
    >>will be used as the implementation for the interface in the derived class.
    >>
    >>In other words:
    >>Option Strict On
    >>
    >>Public Interface ITest
    >> Function Foo() As Integer
    >>End Interface
    >>
    >>Public Class TestBase
    >> Implements ITest
    >>
    >> Public Function Bar() As Integer
    >> Return 100
    >> End Function
    >>
    >> Friend Overridable Function Foo() As Integer Implements ITest.Foo
    >> Return 1
    >> End Function
    >>End Class
    >>
    >>Public Class TestDerive
    >> Inherits TestBase
    >>
    >> Friend Overrides Function Foo() As Integer
    >> Return 2
    >> End Function
    >>End Class
    >>
    >>
    >>I haven't thought too much about this while working with VB.NET so far,

    >so
    >>I'll have to dig into the docs a bit more to see how it's supposed to work,
    >>and why there is a limitation in reimplementation of a common interface

    >on
    >>inherited classes.
    >>
    >>-Rob
    >>
    >>
    >>"Ed Pinto" <ed.pinto@eds.com> wrote:
    >>>
    >>>I've been trying to reimplement an interface in vb, but the compiler won't
    >>>let me.
    >>>
    >>>Class Base implements ITest. Class Derived inherits from Class Base and
    >>>tries to implement ITest, but can't.
    >>>
    >>>I'd like to know if this is because I've got improper scoping or visibility
    >>>issues or if it simply is not possible in vb.
    >>>
    >>>I'm puzzled because C# let's me do this without a problem. And this is

    >>so
    >>>fundamental that I'd be really surprised if this feature was not in both
    >>>languages.

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center