Solved

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

Posted on 2011-03-10
8
566 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
A short film showing how OnPage and Connectwise integration works.

930 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

13 Experts available now in Live!

Get 1:1 Help Now