3

I manage an application that allows the users to automate tasks by writing their own VB code. The user code is compiled using the VBCodeProvider and invoked against the running instance of the application. We've been doing this for a few years now starting with .NET 2.

Traditionally, we have imported the System namespace in the compiler settings so users wouldn't have to write System. all the time. When we went to .NET 4, however, we found that statements like Windows.Forms.Form wouldn't compile anymore. The error was "Type 'Windows.Forms.Form' is not defined." This is odd because other namespaces work. IO.Stream and Reflection.Assembly do not have an error without the System at the beginning.

I've created a simple example. I've put the below code into a file. Then I compiled this file with vbc.exe from both the .NET2 and .NET4 directories. The 2 version works fine. The 4 version will not compile unless you comment out the variable f2.

Imports System

Public Class MyClassName

    Public Shared Sub Main
        'this works in v2 and v4
        Dim f As New System.Windows.Forms.Form
        f.ShowDialog

        'this does not work in v4
        Dim f2 As New Windows.Forms.Form
        f2.ShowDialog
    End Sub

End Class

Does anyone know how to get this to compile in vbc.exe version 4? And before you say "Just tell the users to type System.Windows.Forms" I will agree that it would be great if they would do that, but users do what users do and I have to work it out.

UPDATE:

I've found that the Windows.Foundation.Diagnostics namespace is causing a collision with the abbreviated use of Window.Forms. Is there any way to hide this namespace from my compilation? Visual Studio 2010 does not have the same conflict, so it must be getting around it somehow.

  • Hmm, no, this has not changed. The usual explanation is that there is a "windows" identifier in scope. So the compiler gives up at "Windows.Forms", that "windows" identifier doesn't have a Forms member. – Hans Passant Nov 22 '16 at 18:27
  • There may be such an identifier @HansPassant, but I didn't create one. If it's part of the .Net framework, how does VS2010 compile something like "Windows.Forms.Form" without a similar error? – wernerwichita Nov 28 '16 at 16:14
  • Not in the framework, in the program you compile. Since you appear to allow users to type anything they want, them adding a variable or method named "windows" is an easy mistake of course. Users do what users do. If you need to support them making mistakes like that then maybe automatically uploading their program to your machine would be wise. – Hans Passant Nov 28 '16 at 16:24
  • True @HansPassant the users make all manner of errors. But my sample program above doesn't include anything named "windows" and it fails to compile with vbc.exe for reasons I can't understand. – wernerwichita Nov 28 '16 at 17:18

0 Answers0