Code in VB.NET
Module Utils
<Runtime.CompilerServices.Extension()> _
Public Sub Print(ByVal message As String)
Console.WriteLine(message)
End Sub
End Module
Public Class Foo
Public Sub New()
Dim test = "cucu"
test.Print() ' no problem: custom string extension method '
"cucu".Print() ' syntax error '
End Sub
End Class
1) First problem, I'd prefer to be able to use "cucu".MyExtensionMethod() as well as test.MyExtensionMethod();
1') Syntax like
"No Result".Print() ' Print is an extension method '
" No Result ".Trim() ' Trim is a framework method '
does not work both of them
However, syntax like
myTextBox.Text = "No Result".Translate() ' Translate is an extension method '
myTextBox.Text = " No Result ".Trim() ' Trim is a framework method '
works very well.
So seems some consistency miss of the string constant behavior.
2) Have a look on the COMMENTS (in the attached picture). The words "custom", "string" and "error" are highlighted, however they are in the comments, so should be green, not blue.
Why this? What workaround?
EDIT:
Declared as "bug" in Microsoft Connect (even if is not more that a syntactic "miss")...
EDIT 2:
As remarked Hans Passant, standard string methods, like "cucu".Trim()
does not work either.
Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.
Visual Basic Extension (VBX) is a Visual Basic add-on that Microsoft is converting to an Object Linking and Embedding (OLE) Custom Controls (OCX) infrastructure.
Entity Framework Extensions is a library that dramatically improves EF performances by using bulk and batch operations. People using this library often report performance enhancement by 50x times and more! The library is installed through NuGet. Extension methods are added automatically to your DbContext.
I can confirm this is indeed a “bug” (tested in Visual Studio 2008). But in fact, it’s by design in VB and won’t be changed.
However, I’d like to take the time to explain why this is a horrible question. Sorry Serhio.
Infer
here – it detracts from the problem)Here’s a complete example, using the default settings for VB, that doesn’t have these problems (create a new empty console project solution and paste this code into Module1.vb
):
Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Sub ShowDialog(ByVal message As String)
Console.WriteLine(message)
End Sub
End Module
Module Module1
Sub Main()
Dim s As String = "Hello"
s.ShowDialog()
' Doesn’t work:
'"World".ShowDialog()
' Works:
Call "World".ShowDialog()
End Sub
End Module
The behaviour is consistent in VB: you cannot have a value as the first token in a logical line. For instance, the following code also doesn’t compile (given an existing, appropriate, definition of a form class Form1
):
New Form1().ShowDialog()
and once again the fix is to prefix the expression by Call
:
Call New Form1().ShowDialog()
You can do CStr("cucu").ShowDialog()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With