tags:

views:

20

answers:

2

I have an ASP.NET web site project where I am using both VB.Net and C# class files. I have included separate sub folders in the App_Code directory for classes of each language.

However, while I can successfully make use of a C# class in a VB class, I cannot do the opposite: use a a VB class in a C# class.

So, to illustrate, I might have two classes such as this:

Public Class VBTestClass
    Public Sub New()
    End Sub

    Public Function HelloWorld(ByVal Name As String) As String
        Return Name
    End Function
End Class

public class CSTestClass
{
    public CSTestClass()
    {
    }
    public string HelloWorld(string Name)
    {
        return Name;
    }

}

I can make use of the CS class in my VB class, with the "Imports" statement. So this works well:

Imports CSTestClass
Public Class VBTestClass
    Public Sub New()
    End Sub

    Public Function HelloWorld(ByVal Name As String) As String
        Return Name
    End Function

  Private Sub test()
      Dim CS As New CSTestClass
      CS.HelloWorld("MyName")
   End Sub
End Class

But making use of the VB class in my C#, with the "using" statement, does not work:

using VBTestClass;
public class CSTestClass
{
      public CSTestClass()
      {
  }

     public string HelloWorld(string Name)
    {
        return Name;
    }
}

I get an error that "the type or namespace "VBTestClass" could not be found". What am I missing here?

A: 

the using statement is for namespaces not class names, put the VBClass inside a namespace and then, use the "using" statement:

Namespace MyFoo
Public Class VBTestClass 
    Public Sub New() 
    End Sub 

    Public Function HelloWorld(ByVal Name As String) As String 
        Return Name 
    End Function 
End Class
End Namespace

now in c#:

using MyFoo;

...
David
I tried this, but I get the same problem of "type or namespace not found"...???
Proposition Joe
A: 

THe difference is in how the Imports keyword works compared to the using keyword.

The using keyword can only be used to specify namespaces, while the Imports keyword can also be used to specify classes.

So, Imports CSTestClass specifies that classes, interfaces and enums inside that class should be available, but the class doesn't contain any of those, so the Imports statement is not needed.

When you try to use using VBTestClass it won't work, as VBTestClass is not a namespace.

So, just remove the Imports and using statements, and it should work fine. As the classes are in the same assembly, they already know about each other.

Guffa
Many thanks - very useful info. However, I've tried to both wrap the VB class in a namespace (as per the previous answer), and to remove the Imports and Using statements (just making use of the classess directly on the assumption that they would know each other being in the same assembly). However, I still get the same problem...
Proposition Joe