is there anyway to extend upon an existing Interface?
i'm just wondering.
Printable View
is there anyway to extend upon an existing Interface?
i'm just wondering.
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.
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?
If you are talking about extending like with using the Extension attribute, then of course not. How would you expect that to work?
or something?!Code:+.Method()
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?
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.
im getting this error: Operator '=' is not defined for types 'Integer' and System.IComparableCode:<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 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
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.
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:
Public Class ComplexNumber Private _real As Integer Private _imaginary As Integer Public Property Real() As Integer Get Return Me._real End Get Set(ByVal value As Integer) Me._real = value End Set End Property Public Property Imaginary() As Integer Get Return Me._imaginary End Get Set(ByVal value As Integer) Me._imaginary = value End Set End Property Public Sub New() Me.New(0, 0) End Sub Public Sub New(ByVal real As Integer, ByVal imaginary As Integer) Me._real = real Me._imaginary = imaginary End Sub Public Overrides Function ToString() As String Return String.Format("{0}{1}{2}i", _ Me._real, _ If(Me._imaginary < 0, _ String.Empty, _ "+"), _ Me._imaginary) End Function Public Shared Operator +(ByVal value1 As ComplexNumber, ByVal value2 As ComplexNumber) As ComplexNumber Return New ComplexNumber(value1.Real + value2.Real, value1.Imaginary + value2.Imaginary) End Operator Public Shared Operator +(ByVal value1 As ComplexNumber, ByVal value2 As Integer) As ComplexNumber Return New ComplexNumber(value1.Real + value2, value1.Imaginary) End Operator Public Shared Operator +(ByVal value1 As Integer, ByVal value2 As ComplexNumber) As ComplexNumber Return New ComplexNumber(value1 + value2.Real, value2.Imaginary) End Operator End Class
ok thanks for the example, i think i've got a grasp of it
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.
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.