Re: Problem extending Object

This is a discussion on Re: Problem extending Object within the CSharp forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > CSharp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 08:44 PM
J. Moreno
Guest
 
Default Re: Problem extending Object

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
Reply With Quote
  #2  
Old 08-27-2008, 10:05 PM
Nicholas Paldino [.NET/C# MVP]
Guest
 
Default Re: Problem extending Object

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


Reply With Quote
  #3  
Old 08-27-2008, 10:14 PM
Peter Duniho
Guest
 
Default Re: Problem extending Object

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.
Reply With Quote
  #4  
Old 08-28-2008, 10:41 PM
J.B. Moreno
Guest
 
Default Re: Problem extending Object

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
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 01:14 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.