Solved

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

Posted on 2011-03-10
8
565 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:gbmcneil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much for your help. I will become an expert myself soon.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now