0

I have successfully access a class from VB to C# when I put the VB files to a folder and write this line of codes to web.config

<compilation>
  <codeSubDirectories>
     <add directoryName="VB" />
     <add directoryName="CSharp" />
  </codeSubDirectories>
</compilation>

Now for some reason, I don't want to put the VB files to a sub directory and just put it to App_Code, like this.

However, I'm having an error when I'm trying to access the class from VB to C#. Here.

enter image description here

My question is, is it possible to access the class from VB to C# without putting the VB files to a sub directory? What should I add to web.config to make them connected? Thank you.

Rich
  • 3,928
  • 4
  • 37
  • 66
  • Try using reference to `App_Code` namespace, e.g. `using Website.App_Code`, then you can use `App` class. If it still doesn't work, set build action for VB files inside `App_Code` directory to `Compile`. – Tetsuya Yamamoto Jul 10 '17 at 06:46
  • @TetsuyaYamamoto Actually I already tried that too and still can't. Just giving an error _namespace could not be found_ . By setting a build action do you mean by this? [https://stackoverflow.com/questions/6498245/how-to-set-build-action-on-a-file-advanced-properties-not-displayed-in-propert] – Rich Jul 10 '17 at 06:53
  • You're right, that's where the "Compile" option you should set to all VB code files inside App_Code directory **first** before trying to reference App class using its namespace (`using Website.App_Code`). Give a shot & see if it works properly. – Tetsuya Yamamoto Jul 10 '17 at 06:56
  • @TetsuyaYamamoto Can't find the Build Action – Rich Jul 10 '17 at 07:33
  • Right-click your VB code file, then choose Properties. There is an option called `Build Action` in Properties window and ensure it already set to "Compile". Also check if VB namespaces are arranged properly (set them to `Website.App_Code` if isn't). – Tetsuya Yamamoto Jul 10 '17 at 07:38

1 Answers1

0

First of all, right-click every VB code file and navigate to "Properties" window to see properties of each file, then ensure all of them already have Build Action set to Compile mode such like this:

Build Action Compile

If you're copied VB.NET code files from other location to App_Code directory or found using any different namespace(s), don't forget to set their current namespace properly:

Namespace Website.App_Code
    Module App
       ...
       ' App class contents here

       ...
    End Module
End Namespace

Then, you can use Website.App_Code.App class by referencing its namespace:

using Website.App_Code; 

public class Practice
{
    public static string AttachmentDirectory => App.Attachment;
}

Note that App class needs to be set as public scope so that it can be accessed by all instances across your project.

NB: The AttachmentDirectory property seems assigned with expression-bodied member syntax which applicable to version 6.0 or above. Check for currently used compiler version to use that kind of syntax.

Similar issues:

Classes residing in App_Code is not accessible

How to call a class in the App_Code folder from code behind?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I don't know the problem, but there's no "Properties" item in the menu when I right click a VB file. – Rich Jul 10 '17 at 09:43
  • By the way I'm using ASP.net web forms, not ASP.net MVC. May I know the version of your VS? – Rich Jul 10 '17 at 09:52
  • Currently I've using VS 2013 & 2015, and Properties context menu still exist for both editions. If you can't use properties from context menu, use View => Properties Window to display the window and just click VB file you want to see its properties. – Tetsuya Yamamoto Jul 10 '17 at 10:00
  • Yeah, I already tried that either. But still there's no build action drop down. – Rich Jul 11 '17 at 02:35