Solved

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

Posted on 2011-03-10
8
571 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
  • 4
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 125 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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