0

In Visual Studio, I have a solution containing both a Visual Basic (VB) project (called WindowsVB) and a C# project (called Windows_C_Sharp). Within the C# project, I want to use classes/modules declared in the VB project.

I know there are other questions and answers addressing this issue, but as far as I see I have followed these answers correctly, and it still doesn't work.

The following screen print shows what goes wrong. As you can see, I have added the reference to the VB project (as seen in the solution explorer to the left). But when I try to import the VB project into my C# class (in this case it's actually the main method in program.cs) with a 'using' statement, that VB project isn't recognized - as indicated by the red underlining of the word 'using', as well as by the error ("type or namespace WindowsVB could not be found") shown in the Error List window.

Here's the screen print (click to enlarge):

enter image description here

As mentioned here: How to call a function written in VB from C# application in the answer provided by the user called 'awe', by default the namespace to be imported is the name of the VB project.

So my question is, what am I doing wrong here? Why isn't the namespace provided for the VB project (again, that namespace being the name of that project) recognized?

Community
  • 1
  • 1
Holland
  • 395
  • 9
  • 22
  • Does vb project has that namespace ? – Chetan May 05 '17 at 22:57
  • @ChetanRanpariya: Well, to be brutally honest, I don't know -- I've never worked with Visual Basic before. I was just going by the answer mentioned in my question, where it is stated that the namespace is equal to the name of the VB project, at least by default. If you can tell me where I can find and/or change the VB namespace, that would be useful. In C# the definition of the namespace is of course very easy to find, but apparently it works differently in VB... At least I don't see namespaces defined in the class definitions... (I have a lot to learn, I know.) – Holland May 05 '17 at 23:08
  • 3
    Use Object Browser to see the namespace(s) used in the vb.net code. It isn't always obvious because VB.NET code tends to end up in the global namespace. – Hans Passant May 05 '17 at 23:51
  • @Holland did you try accessing classes and modules of vb project directly? – Chetan May 06 '17 at 00:24
  • It looks to me like you may have changed the name of at least the C# project after you created it. Did you do the same with the VB project? Despite the fact that your C# project is named "Windows_C_Sharp", the default namespace is "WindowsFormsApplication1", indicating that that was the original name of the project. If you changed the name of your VB project too then the root namespace for the project will be the original name, not the current name. Changing the project name does not affect the root namespace. You must change that separately in the project properties. – jmcilhinney May 06 '17 at 01:47
  • Thanks all. @jmcilhinney: That was exactly it, you nailed it! I've now succesfully changed the namespace in the project properties and it works now! Many thanks and if you can turn your comment into an answer I will accept it. – Holland May 06 '17 at 04:55
  • @Hans Passant: Yes, I also found the namespaces in the Object Browser, very useful as I wasn't even aware of this browser. – Holland May 06 '17 at 04:57

1 Answers1

1

The project name and the root namespace are two different things. When you create a project and name it, that name is used for the default namespace too, but it's still store in a different location. If you change the name of the project, the root namespace is unaffected. It must be changed separately in the project properties.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46