views:

3013

answers:

9

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.
+2  A: 

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")
ICR
Thanks, I know what and how Extension methods work. My extensions work fine from other C# project however, as my original question stated, the do not appear and and will not compile in VB project that reference the same DLL's
Greg B
A: 

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.

Jason Jackson
A: 

… 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()
Konrad Rudolph
Yes, I'm importing the full namespace, and by "old" I simply mean It's a site that I didn't write and hence, is in VB.A casual "old" rather than an technically "old" site
Greg B
+1  A: 
Imports x.y

'...'
Dim r As String = "greg"
Dim s As String = r.q() 'same as z.q(r)
David Kemp
+2  A: 

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'.

Konrad Rudolph
It was so long ago I can't remember the actual issue
Greg B
+5  A: 

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?

Jon Skeet
I think you may be right about it being ReSharper. I don't get it grayed out.
ICR
A: 

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.

David Kemp
A: 

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.

Greg B
A: 

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.

NagyBandi