Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Msgbox problem (VB newbie) 2

Status
Not open for further replies.

Doswelk

IS-IT--Management
Joined
Apr 3, 2001
Messages
72
Location
GB
I am just working through the basics of VB.Net and I have hit a very strange problem:

If I type MsgBox(&quot;<anything>&quot;) in any new projects I create it under lines it and gives me an error message:

'MsgBox' is a namespace, and so is not a valid expression.

This only just started happening Inputbox and other commands (not that I have tried many others) still work and uninstalling made no difference any ideas ?
 
I worked it out!

I am working through a book and it told me to create a new project and call it MsgBox and it is only that program that does not work!

Should this happen ?
 
Use Messagebox.show(&quot;yourmessage&quot;)
 
With a library as big as the one .Net has it is highly likely that two objects would have the same name. The compiler would not know which one you were talking about. .Net uses namespaces to help uniquely identify classes. A class belongs to a namespace this is placed before the class name to clarify the situation. It is rather like using a COM ProgID in VB6.

In order to prevent you having to type in namespaces all the time you can use an Imports statement at the top of your code file. This following syntax:

Imports [ <aliasname> = ] <namespace>

To save you even more time VB automatically imports a number of namespaces into all your code files. A list of these can be found in the project settings.

When writing your own classes you might want to put your code in your own namespaces. This can be done using the namespace keyword:

Namespace <name>
<class definitions>

end namespace

This is a good idea as it helps prevent your names conflicting with other developers’ classes, however it can be a bit tedious. VB.Net comes to the rescue again by allowing you to set a default namespace for all the classes in your project. This is set in project properties and is defaulted to the same name as the project. Hence why MsgBox is a namespace.

The full name of the MsgBox function is Microsoft.VisualBasic.Information.MsgBox but you can omit the Microsoft.VisualBasic assuming you don’t have another namespace called Information. If you use this instead of just MsgBox it should sort out the problem, or you could change the namespace for your project.

Caffein
[afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top