| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Since nobody seems to know the answer in the vb group, guess I'll ask here... Does anyone know why I would be able to create an extension method for Object in C# but not in VB? I understand why using ByRef for the first parameter would work in VB but not C#, but this I just don't get at all... J. Moreno <planb@newsreaders.com> wrote: > Hello, > > I'm trying to add an extension to Object using VB. It shows up in the > intellisense for other kinds of reference objects, but not if the > variable is simply declared as Object. > > It works without problems when I use C#, but not from within VB...any > ideas as to what is going on? > > Imports System.Runtime.CompilerServices > > Module StringUtils > > <System.Runtime.CompilerServices.Extension()> _ > Public Function eMyE(ByVal o As System.Object, _ > ByVal sep As Char) As String > Return "1" > End Function > <System.Runtime.CompilerServices.Extension()> _ > Public Function eMyE(ByVal o As System.Object) As String > Return o.eMyE(" ") > End Function > End Module > > Public Class Form1 > > Private Sub Button1_Click(ByVal sender As System.Object, _ > ByVal e As _ > System.EventArgs) Handles Button1.Click > Dim b As String ' sender.eMyE doesn't show up in intellisense > End Sub > End Class > > ## and here it is in C# > > using System; > using System.Runtime.CompilerServices; > using System.Windows.Forms; > > namespace TestExtensionsC > { > public static class StringUtils > { > public static string eMyE(this object str, char sep) > { > return "1"; > } > > public static string eMyE(this object str) > { return str.eMyE(' '); } > } > > public partial class Form1 : Form > { > > public Form1() > { > InitializeComponent(); > } > > private void button1_Click(object sender, EventArgs e) > { > string b = sender.eMyE(); > } > } > } -- J. Moreno |
|
#2
| |||
| |||
| It's basically to prevent late bound code from breaking: http://blogs.msdn.com/vbteam/archive...ds-part-4.aspx -- - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "J. Moreno" <planb@newsreaders.com> wrote in message news:20080827204500.258$hC@newsreader.com... > Since nobody seems to know the answer in the vb group, guess I'll ask > here... > > Does anyone know why I would be able to create an extension method for > Object in C# but not in VB? I understand why using ByRef for the first > parameter would work in VB but not C#, but this I just don't get at all... > > > J. Moreno <planb@newsreaders.com> wrote: >> Hello, >> >> I'm trying to add an extension to Object using VB. It shows up in the >> intellisense for other kinds of reference objects, but not if the >> variable is simply declared as Object. >> >> It works without problems when I use C#, but not from within VB...any >> ideas as to what is going on? >> >> Imports System.Runtime.CompilerServices >> >> Module StringUtils >> >> <System.Runtime.CompilerServices.Extension()> _ >> Public Function eMyE(ByVal o As System.Object, _ >> ByVal sep As Char) As String >> Return "1" >> End Function >> <System.Runtime.CompilerServices.Extension()> _ >> Public Function eMyE(ByVal o As System.Object) As String >> Return o.eMyE(" ") >> End Function >> End Module >> >> Public Class Form1 >> >> Private Sub Button1_Click(ByVal sender As System.Object, _ >> ByVal e As _ >> System.EventArgs) Handles Button1.Click >> Dim b As String ' sender.eMyE doesn't show up in intellisense >> End Sub >> End Class >> >> ## and here it is in C# >> >> using System; >> using System.Runtime.CompilerServices; >> using System.Windows.Forms; >> >> namespace TestExtensionsC >> { >> public static class StringUtils >> { >> public static string eMyE(this object str, char sep) >> { >> return "1"; >> } >> >> public static string eMyE(this object str) >> { return str.eMyE(' '); } >> } >> >> public partial class Form1 : Form >> { >> >> public Form1() >> { >> InitializeComponent(); >> } >> >> private void button1_Click(object sender, EventArgs e) >> { >> string b = sender.eMyE(); >> } >> } >> } > > -- > J. Moreno |
|
#3
| |||
| |||
| On Wed, 27 Aug 2008 19:05:59 -0700, Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard.caspershouse.com> wrote: > It's basically to prevent late bound code from breaking: > > http://blogs.msdn.com/vbteam/archive...ds-part-4.aspx Seems like that's something they should have mentioned in the documentation. |
|
#4
| |||
| |||
| Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard.caspershouse.com> wrote: -snip why extension method for C# works, but same method in VB doesn't- > It's basically to prevent late bound code from breaking: > > http://blogs.msdn.com/vbteam/archive...s-and-late-bin > ding-extension-methods-part-4.aspx Thanks, That at least settles the question as to what's happening and why... -- J.B. Moreno |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.