?
Solved

Why can't I say Public Sub DoThis() End Sub in a VB.NET code module?

Posted on 2011-03-10
8
Medium Priority
?
591 Views
Last Modified: 2012-05-11
Experts -

Why can't I say:

Public Sub DoThis()
    blah..
End Sub

 in a VB.NET code module? I get an error that says "Statement is not valid in a Namespace."
0
Comment
Question by:gbmcneil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35101697
Hi,

Yes, you can do it as long as the Sub is inside of the Module, like this

Namespace YOURNAMESPACE
  Module Module1

     Public Sub DoThis()
    End Sub

  End Module
End Namespace
0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35101718
Hi,

You have to place your code inside the Module and not the Namespace section
Namespace Bogus

    ' Dont place sub here

    Module Module1
        Public Sub DoThis()

        End Sub
    End Module

End Namespace

Open in new window

0
 

Author Comment

by:gbmcneil
ID: 35101901
Thanks for responding, gamarrojqq.

Unlike creating a class, VB.NET gives you a bare screen when a Module is created. It doesn't come back and say Public Module Module1. Just a bare screen.

So, you're saying that I should add:

Namespace Namespace1            (not "Public Namespace Namespace1")

and

Module Module1                           (not "Public Module Module1")

I guess they all would have to be assumed to be "Public" because what the heck could you do with it if it were "Private"?

Also, where does the Namespace come in? Do all miscellaneous functions and procedures go in the same Namespace. I'm just loaded with questions.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35101962
Yes, you should add the namespace, and yes  Modules are PUBLIC and do not need to add PUBLIC to declare them

Namespaces are using to group your classes so when you compile your Assembly do not crash with another Assambly, so many people can have an Assenbley with a class named MyModule for example, but not many will have the same namespace, so this

Namespace GAMARROJGQ

 Module MyModule
 End Module

End Namespace

Will allow me to call my Moduloe like this   GAMARROJGQ.MyModule, so it do not match any other MyModule that could be in my PC.

0
 

Author Comment

by:gbmcneil
ID: 35102059
Always the renagade, I did the following and it run perfectly in debug mode.

Public Module Module1

       Public Sub DoThis ()
              blah...
       End Sub

End Module

Now, run that by me again. What am I losing by not defining a Namespace?

Are you saying that Namespaces support development of executables by more than one programmer?
0
 

Author Comment

by:gbmcneil
ID: 35102163
If so, does that mean that every Plublic Class Class1 and every Public Form Form1 should be associated with a Namespace (i.e., should have a Namespace defined at the very top of their respective code files)?
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35102719
Yes, Namespaces are optionals, but  if you plan to deploy a Class Library Project (DLL) in a more formal/professional way, you should definitively use them.

Look at this link

http://visualbasic.about.com/od/usingvbnet/a/nmspc.htm
0
 

Author Closing Comment

by:gbmcneil
ID: 35104082
Thanks very much for your help. I will become an expert myself soon.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question