471,325 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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 <pl***@newsreaders.comwrote:
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
Aug 28 '08 #1
3 1219
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]
- mv*@spam.guard.caspershouse.com

"J. Moreno" <pl***@newsreaders.comwrote in message
news:20*******************@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 <pl***@newsreaders.comwrote:
>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
Aug 28 '08 #2
On Wed, 27 Aug 2008 19:05:59 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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.
Aug 28 '08 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:

-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
Aug 29 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alicia Haumann | last post: by
7 posts views Thread by A Traveler | last post: by
4 posts views Thread by Brandon Miller | last post: by
5 posts views Thread by vbgunz | last post: by
4 posts views Thread by Ian Richardson | last post: by
2 posts views Thread by Spondishy | last post: by
3 posts views Thread by Jeff | last post: by
3 posts views Thread by katis | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.