[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
?
596 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

656 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