1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
R Powell
  • 99
  • 1
  • 10
  • Does your project that contains `TestClass` have a Root namespace specified in the project properties? Also, if you're getting errors, you should include them in your question. – Chris Dunaway Sep 06 '19 at 14:10
  • To avoid any ambiguity due to VB's default Namespace, consider rooting your added Namespace to the [Global Namespace](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/namespaces#global-keyword-in-namespace-statements). – TnTinMn Sep 07 '19 at 00:40

2 Answers2

4

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.
InteXX
  • 6,135
  • 6
  • 43
  • 80
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Joel, Thanks for your response. Unfortunately, the full namespace approach (step #2) also fails. There is no option for 'WebApp1.SignInSignOut'. Indeed the new file was placed inside the App_Code folder. Yes, I realize the MsgBox method will break a web app on a production server; however, it works fine while debugging with localhost. – R Powell Sep 06 '19 at 16:04
2

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.

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Is there any setting that can add namespace in newly added files as per folder structure? – Haider Ali Wajihi Oct 27 '21 at 01:52
  • @HaiderAliWajihi looks like there is an answer for this https://stackoverflow.com/questions/3317141/automatically-add-namespace-when-adding-new-item – T.S. Oct 27 '21 at 02:39