A.R.
A.R.

Reputation: 15685

Class from c# assembly not visible to VB

I have an assembly which was written in C#. Inside of it, I have a number of static classes that have static functions etc. Referencing these functions from another c# project works just fine, but if I try to use them in a VB.NET project, the classes are not available / visible at all, even though they appear in the object browser, and the project will compile / run as long as I don't reference said classes.

Here is how I define the class in C#, for example...

namespace Tools
{
    public static class MyTools
    {
        public static int SumNumber(int n1, int n2) { return n1 + n2; }
    }
}

Attempting to use the class in VB will result in the error:

'MyTools' is not declared. It may be inaccessible due to its protection level.

Attempts to use a fully qualified name 'Tools.MyTools.SumNumber....' will result in the exact same error message.

I have checked, both the C# assembly and the VB project are targeting .NET 4.5 What am I missing here?

Upvotes: 1

Views: 1668

Answers (2)

A.R.
A.R.

Reputation: 15685

I cleared the 'Root Namespace' option in the project properties and now everything is working fine. Leaving it in leads to non-obvious namespace nesting that confuses programmers.

Upvotes: 1

Bradley Uffner
Bradley Uffner

Reputation: 16991

This problem is typically due to not using the proper namespace when you try to use the class. Get the full namespace from the C# code then use that namespace to fully qualify the class name when calling it on the VB side. If that works you should either import that namespace at the project or file level, or rework your namespaces for easier access.

If that still doesn't work make sure you have a proper reference to the c# project from the VB project. Make sure you didn't accidentally reference a copy of the output dll or some other stale file.

Upvotes: 0

Related Questions