Reputation: 99
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.....
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
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
Reputation: 415630
Four things I noticed so far:
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
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
ASP.NET Webforms is picky about where that code should live. Put it in the App_Code
folder in the solution.
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
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