|
-
Nov 6th, 2009, 12:03 PM
#1
Thread Starter
Hyperactive Member
extend an interface
is there anyway to extend upon an existing Interface?
i'm just wondering.
-
Nov 6th, 2009, 12:14 PM
#2
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.
-
Nov 6th, 2009, 12:34 PM
#3
Thread Starter
Hyperactive Member
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?
-
Nov 6th, 2009, 01:53 PM
#4
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?
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?
-
Nov 6th, 2009, 02:14 PM
#5
Thread Starter
Hyperactive Member
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
-
Nov 6th, 2009, 04:37 PM
#6
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
 
-
Nov 7th, 2009, 04:27 AM
#7
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:
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
-
Nov 7th, 2009, 11:34 AM
#8
Thread Starter
Hyperactive Member
Re: extend an interface
ok thanks for the example, i think i've got a grasp of it
-
Nov 7th, 2009, 03:57 PM
#9
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
 
-
Nov 7th, 2009, 07:28 PM
#10
Re: extend an interface
 Originally Posted by Shaggy Hiker
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.
-
Nov 7th, 2009, 09:51 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|