472,984 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 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 1273
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.