R Powell
R Powell

Reputation: 99

Can't create a new namespace in VB.Net project using VS 2019

I am attempting to define new namespaces and classes within a new VB.NET project; however, Visual Studio is not allowing me to instantiate any of my new namespaces/classes/methods.

I have.....

  1. set up a new project in Visual Studio 2019 Community;
  2. created a new namespace (SignInSignOut) with a single class (TestClass) and one method (ShowMessageBox);
  3. attempted to Import the new namespace into an existing code-behind page (Default.aspx.vb); and
  4. attempted to instantiate the new class and method from the Page_Load() method of the code-behind page.

The attempt to Import the new namespace into the existing code-behind page fails.

The new Namespace, Class and Method.....

Imports Microsoft.VisualBasic

Namespace SignInSignOut
    Public Class TestClass
        Public Shared Sub ShowMessageBox(ByVal sString As String)
            MsgBox(sString)
        End Sub
    End Class
End Namespace

The attempt to import the Namespace.Class and instantiate the Method on the Default.aspx.vb code-behind page.....

Imports SignInSignOut.TestClass

Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        ShowMessageBox("This is just a test!")

    End Sub
End Class

Edit

I resolved the issue by re-installing Visual Studio 2019 Community. I had reinstalled an old copy of VS 2010 Pro and the example worked as expected, which led me to believe I might have a problem with VS 2019 Community.

Upvotes: 1

Views: 1596

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415630

Four things I noticed so far:

  1. Import namespaces, not classes (at least for the moment):

    Imports SignInSignOut
    
    Public Class _Default
        Inherits Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
            TestClass.ShowMessageBox("This is just a test!")
    
        End Sub
    End Class
    
  2. Make sure that's the full name for the namespace, and you haven't inherited a higher-level namespace from your project:

    Imports MyProjectName.SignInSignOut
    
  3. ASP.NET Webforms is picky about where that code should live. Put it in the App_Code folder in the solution.

  4. MsgBox() won't do what you expect in a webforms project. It will open a message box, but it will do it on the web server and not in the browser on the user's machine. The message box will still block the thread until someone dismisses it, which is a problem for your web server.

Upvotes: 4

T.S.
T.S.

Reputation: 19330

In visual basic, generally, namespaces works in 2 parts (unlike c#). When you create project, it automatically creates a namespace for you, which you can see in project properties - Default Namespace. This is where you initially will find same value as in Assembly Name and original project name. for now, lets assume, this name is MyWebProject

Now, as you add a new file, it will not add Namespace block. You have automatic namespace MyWebProject. If you add namespace block, like MyFirstNamespace, your class within this block will be - MyWebProject.MyFirstNamespace, hence 2-part namespace in VB.net.

Upvotes: 2

Related Questions