Results 1 to 11 of 11

Thread: extend an interface

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    376

    extend an interface

    is there anyway to extend upon an existing Interface?

    i'm just wondering.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: extend an interface

    Extend in what sense? Extension methods can be written for interfaces, and an interface can inherit from (and only) another interface.

    Perfect example of both is the IEnumerable(Of T) interface which is inherited from the IEnumerable interface and most of the LINQ extension methods are written for this interface.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    376

    Re: extend an interface

    is this the correct way to extend its methods: <Runtime.CompilerServices.Extension()> Public Sub extension(ByVal integer_instance As icomparable)


    anyway, is there anyway to extend its operators?

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: extend an interface

    If you are talking about extending like with using the Extension attribute, then of course not. How would you expect that to work?
    Code:
    +.Method()
    or something?!

    Every single one of your posts is very odd and tries to use the programming language in ways that it is not intended. Why are you insisting on doing this so weirdly? What are you expecting to gain from that?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    376

    Re: extend an interface

    well adding an operator like a + b, or a=b, or a*b
    its like integer have the operator + - * / and so on, i was wondering how we could extend an existing interface with these operators

    here's what i'm trying to do.
    Code:
        <Runtime.CompilerServices.Extension()> Public Function inside(ByVal integer_instance As Integer, ByVal ParamArray ARRAY_OF_number_or_string_or_char_that_is_going_to_be_checked() As icomparable) As Boolean
            For Each number_or_string_or_char_that_is_going_to_be_checked In ARRAY_OF_number_or_string_or_char_that_is_going_to_be_checked
                If number_or_string_or_char_that_is_going_to_be_checked.GetType.Name <> "String" And number_or_string_or_char_that_is_going_to_be_checked.GetType.Name <> "Char" And integer_instance = number_or_string_or_char_that_is_going_to_be_checked Then
                    Return True
                End If
            Next
            Return False
        End Function
    im getting this error: Operator '=' is not defined for types 'Integer' and System.IComparable

    im thinking how could i add that in

    if i change both to strings it would work fine, but i think dat its nt a gd solution if i could sort of get the '=' operator working

    here's an example of changing both to strings: (this version works, i'm trying to improve it)
    Code:
        <Runtime.CompilerServices.Extension()> Public Function inside(ByVal integer_instance As Integer, ByVal ParamArray ARRAY_OF_number_or_string_or_char_that_is_going_to_be_checked() As icomparable) As Boolean
            For Each number_or_string_or_char_that_is_going_to_be_checked In ARRAY_OF_number_or_string_or_char_that_is_going_to_be_checked
                If number_or_string_or_char_that_is_going_to_be_checked.GetType.Name <> "String" And number_or_string_or_char_that_is_going_to_be_checked.GetType.Name <> "Char" And integer_instance.ToString = number_or_string_or_char_that_is_going_to_be_checked.ToString Then
                    Return True
                End If
            Next
            Return False
        End Function

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: extend an interface

    What you'd probably like to have is operator overloading. Not yet available in VB, though. Would be a good feature, and sure to show up, eventually.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: extend an interface

    First up, you can define extension methods on interfaces. You have to be able to because basically the whole reason that extension methods exist is so that you can do fancy things with IEnumerable objects using LINQ.

    That said, extension methods are methods, while operators are not methods, so there's no such thing as an extension operator. Also, operators are Shared and interfaces can't have Shared members.

    Finally, operator overloading is alive and well in VB.NET, e.g.
    vb.net Code:
    1. Public Class ComplexNumber
    2.  
    3.     Private _real As Integer
    4.     Private _imaginary As Integer
    5.  
    6.  
    7.     Public Property Real() As Integer
    8.         Get
    9.             Return Me._real
    10.         End Get
    11.         Set(ByVal value As Integer)
    12.             Me._real = value
    13.         End Set
    14.     End Property
    15.  
    16.     Public Property Imaginary() As Integer
    17.         Get
    18.             Return Me._imaginary
    19.         End Get
    20.         Set(ByVal value As Integer)
    21.             Me._imaginary = value
    22.         End Set
    23.     End Property
    24.  
    25.  
    26.     Public Sub New()
    27.         Me.New(0, 0)
    28.     End Sub
    29.  
    30.     Public Sub New(ByVal real As Integer, ByVal imaginary As Integer)
    31.         Me._real = real
    32.         Me._imaginary = imaginary
    33.     End Sub
    34.  
    35.  
    36.     Public Overrides Function ToString() As String
    37.         Return String.Format("{0}{1}{2}i", _
    38.                              Me._real, _
    39.                              If(Me._imaginary < 0, _
    40.                                 String.Empty, _
    41.                                 "+"), _
    42.                              Me._imaginary)
    43.     End Function
    44.  
    45.  
    46.     Public Shared Operator +(ByVal value1 As ComplexNumber, ByVal value2 As ComplexNumber) As ComplexNumber
    47.         Return New ComplexNumber(value1.Real + value2.Real, value1.Imaginary + value2.Imaginary)
    48.     End Operator
    49.  
    50.     Public Shared Operator +(ByVal value1 As ComplexNumber, ByVal value2 As Integer) As ComplexNumber
    51.         Return New ComplexNumber(value1.Real + value2, value1.Imaginary)
    52.     End Operator
    53.  
    54.     Public Shared Operator +(ByVal value1 As Integer, ByVal value2 As ComplexNumber) As ComplexNumber
    55.         Return New ComplexNumber(value1 + value2.Real, value2.Imaginary)
    56.     End Operator
    57.  
    58. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    376

    Re: extend an interface

    ok thanks for the example, i think i've got a grasp of it

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: extend an interface

    My information is dated. When did operator overloading show up? I knew it was coming, but I thought it was not until the next version. Of course, I may have been thinking that back in 2005 or 2003.
    My usual boring signature: Nothing

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: extend an interface

    Quote Originally Posted by Shaggy Hiker View Post
    My information is dated. When did operator overloading show up? I knew it was coming, but I thought it was not until the next version. Of course, I may have been thinking that back in 2005 or 2003.
    I've got a feeling that it was 2005 but it may have been 2008. Haven't had too much cause to implement it myself so I'm not 100% sure. Obviously you haven't implemented it much either.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: extend an interface

    Yeah, "not at all" certainly counts. I do remember reading that it was going to be added as a feature. I hope I wasn't remembering that from four years back (or more). Operator overloading has a few really useful purposes, but I haven't encountered one in years.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width