Question

I have a class library with some extension methods written in C# and an old website written in VB.

I want to call my extension methods from the VB code but they don't appear in intelisense and I get compile errors when I visit the site.

I have got all the required Imports because other classes contained in the same namespaces are appearing fine in Intelisense.

Any suggestions

EDIT: More info to help with some comments.

my implementation looks like this

//C# code compiled as DLL
namespace x.y {
    public static class z {
        public static string q (this string s){
             return s + " " + s;
        }

    }
}

and my usage like this

Imports x.y

'...'
Dim r as string = "greg"
Dim s as string = r.q() ' does not show in intelisense
                        ' and throws error : Compiler Error Message: BC30203: Identifier expected.
Was it helpful?

Solution

It works for me, although there are a couple of quirks. First, I created a C# class library targeting .NET 3.5. Here's the only code in the project:

using System;

namespace ExtensionLibrary
{
  public static class Extensions
  {
    public static string CustomExtension(this string text)
    {
      char[] chars = text.ToCharArray();
      Array.Reverse(chars);
      return new string(chars);
    }
  }
}

Then I created a VB console app targeting .NET 3.5, and added a reference to my C# project. I renamed Module1.vb to Test.vb, and here's the code:

Imports ExtensionLibrary

Module Test

    Sub Main()
        Console.WriteLine("Hello".CustomExtension())
    End Sub

End Module

This compiles and runs. (I would have called the method Reverse() but I wasn't sure whether VB might magically have reverse abilities already somewhere - I'm not a VB expert by a long chalk.)

Initially, I wasn't offered ExtensionLibrary as an import from Intellisense. Even after building, the "Imports ExtensionLibrary" is greyed out, and a lightbulb offers the opportunity to remove the supposedly redundant import. (Doing so breaks the project.) It's possible that this is ReSharper rather than Visual Studio, mind you.

So to cut a long story short, it can be done, and it should work just fine. I don't suppose the problem is that you're either using an old version of VB or your project isn't targeting .NET 3.5?

As noted in comments: there's one additional quirk, which is that extension methods won't be found when the compile-time type of the target is Object.

OTHER TIPS

Extension methods are just syntactic sugar for static methods. So

public static string MyExtMethod(this string s)

can be called in both VB.NET and C# with

MyExtMethod("myArgument")

OK. Based on the error message you are definitely not using the most recent VB version (VB 9!) or the error isn't related to this problem at all because then you'd get another error if the method wasn't found:

Error 1 'q' is not a member of 'String'.

Imports x.y

'...'
Dim r As String = "greg"
Dim s As String = r.q() 'same as z.q(r) 

I think I've encountered a similar problem: VB.Net is quite happy to compile on through extension methods and leave them to be inferred at run-time if Option Strict is off.

However, VB.Net really doesn't seem to like extension methods on basic types. You can't extend Object and it can't resolve it if you do:

C#

namespace NS
...

public static class Utility {

    public static void Something(this object input) { ...

    public static void Something(this string input) { ...

}

// Works fine, resolves to 2nd method
"test".Something();

// At compile time C# converts the above to:
Utility.Something("test");

However this goes wrong in VB.Net:

Option Infer On
Option Explicit On
Option Strict Off
Imports NS
...

    Dim r as String = "test" 
    r.Something()

That compiles without error, but at run time fails because Something is not a method of String - the compiler has failed to replace the syntactic sugar of the extension method with the the static call to Utility.Something.

The question is why? Well unlike C#, VB.Net can't handle any extension to Object! The valid extension method in C# confuses the VB.Net compiler.

As a general VB.Net rule, I'd avoid using extension methods with any of the basic .Net types (Object, String, Integer, etc). You also have to be careful with Option Infer as while it's on by default in Visual Studio it's off by default for command line compiles, VBCodeProvider, and possibly in web sites (depending on your web.config). When it's off everything in VB.Net is considered to be an Object and all extension methods will be left until run time (and will therefore fail).

I think Microsoft really dropped the ball when they added extension methods to VB.Net, I think it was an afterthought to try (and fail) to make it consistent with C#.

I don't know if you can call them in the same dot notation as you would in C#, but I would think that the static extension methods would show up as static functions with the fist argument as the extended type. So you should be able to call the actually class in VB with:

StaticClass.ExtensionMethod(theString, arg1, ..., argN)

Where in C# you would have just written:

theString.ExtensionMethod(arg1, ..., argN);

With StaticClass being the name of the static class in which you defined your extension methods.

… and an old website written in VB.

Does “old” here imply perhaps that you also use an old version of VB here? Anyway, since extension methods are just vanilla static (“Shared”) methods decorated with an attribute, you should be able to call them in any case.

If this isn't possible you either try to call them “extension style” in an old version of VB or you're referencing the wrong version of your C# assembly.

Edit: are you sure you're Importing the whole namespace, i.e. x.y and not just x? VB is able to access nested namespaces easier than C# so you can use classes from namespace x.y using the following code in VB. However, for extension methods to work, the full path has to be Imported.

Imports x
Dim x As New y.SomeClass()

Two things to check:

  1. You're Targeting .Net 3.5
  2. You're referencing the DLL

Some tools might incorrectly suggest extension methods for projects that don't support them.

I ran into the same problem, and might accidentally have stumbled upon the solution. If I used

x.y.r.q()

, it threw the same error for me as well. But if I imported x.y, it worked, so:

using x.y;
...
r.q()

was fine.

So apparently you have to import it in the declaration to make it work.

This was quite a while ago and I can't really how I solved it, but needless to say, it was user error. I probably restarted my computer and away it went.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top