473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1284
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
7
by: A Traveler | last post by:
Hello all, i was just curious if anyone whos been playing with VS2005 could tell me... In javascript (and java??) you can alter the prototypes for an object in your project. I dont remember...
4
by: Brandon Miller | last post by:
All, I have an existing business object (VB.Net) which returns user IDs for our locations in our regions. One of the properties objReg.Manager returns the manager's user id (integer) for a given...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
2
by: Spondishy | last post by:
Hi, I'm attempting to extend IIdentity to enable me to have more information on the user (email etc) and store it in a cookie. I assume I am extending the correct object (but if not shout up). ...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm trying to extend the MembershipUser class, and have encounter a problem: << See in the middle of this post for info about why I do this >> << See below of this post for...
3
by: katis | last post by:
Hi all :) Is it posible in xsd to change only minOccurs value of element in complex type by extending this type? The problem I'm trying to solve is this: I have two complex types -...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.