Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 9th, 2016, 12:37 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2016
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default namespace query

Need some clarification when it comes to namespaces

Code:
using system;
using static System.Console;
using System.Collections.Generic;
If I am calling up "System" (using System) and "Console" is a part of "System" why do I have to call up "using static System.Console"? (not sure if "call up" is the right verbiage, or is it "including"?). Same thing with "Collections.Generic" please review below.
Code:
namespace System
{
     namespace Console
     {
      
     }
     namespace Collections
     {

          namespace Generic
          {

          }

     }

}
So I visualize that when I bring in System which is the parent all the children would come along for the ride.

Again in layman's terms could I get a better understanding of how namespace works.

Thanks
Reply With Quote
  #2 (permalink)  
Old August 9th, 2016, 01:06 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,145, Level: 23
Points: 3,145, Level: 23 Points: 3,145, Level: 23 Points: 3,145, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 641
Thanks: 2
Thanked 96 Times in 95 Posts
Default

First, no a using statement doesn't bring in sub-namespaces so "using System" does not include "System.Drawing" or any other System sub-namespace.

The purpose of namespaces is to not clutter the program up with symbols. For example, suppose System.Drawing and System.Collections.Generic both define some class. Then you wouldn't want them both included unless you really want to use both. Having them both lying around would make using them confusing. (It would also make compiling the program slower, although it would still run as fast because unused symbols are removed during compilation.)

Second, regarding "using static System.Console." Using "static" with "using" is a fairly new and weird. It lets you use the static methods of a class without naming the class. In this example, it means you can use the Console class's static methods such as Write and WriteLine without naming the class as in:

Code:
WriteLine("Done");
If you omit the "using" statement, then you need to write this:

Code:
Console.WriteLine("Done");
So the "using static System.Console" directive doesn't include a namespace, it makes this special arrangement for the class.

Personally I think that's confusing because the code doesn't show what class owns the WriteLine method. Just looking at the code you would think it was in the current class (probably the form). I think Microsoft added this because it works in other languages rather than because it's something people wanted.

I assume with the other code that you're trying to show the structure of the System namespace (as opposed to trying to create your own namespaces with those names).

In that case, Console is a class not a namespace, so the system structure would look more like this:

Code:
namespace System
{
    public static class Console
    {
        ...
    }
    namespace Collections
    {
        namespace Generic
        {
            ...
        }
    }
    ...
}
But again including System does not include its sub-namespaces. It does include Console so your code can use Console.WriteLine, but you couldn't use classes in the sub-namespaces without including them, too.

One more note about using directives. If you look at them, Visual Studio grays out the ones that you aren't actually using. For example, if you include System.Collections but don't use any classes or other items defined in it, then it's grayed out.

You can remove them by right-clicking, picking Organize Usings, and then picking, Remove Unnecessary Usings.

That removes some clutter and, as I mentioned above, will speed up compiling a little, but won't change execution. (I usually just leave the unused ones that Visual Studio adds when it starts the project.)

I hope that helps. If it's still unclear, let me know.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old August 9th, 2016, 01:28 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2016
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I believe I understand, however here is another confusing thing

Code:
using  System.Convert;

myDirection = ToInt32(ReadLine());
----------------------------------------
ToInt32() throws an "The name 'ToInt32' does not exist in the current context", unless I use static in the using statement as below.

Code:
using static System.Convert;

myDirection = ToInt32(ReadLine());
Thanks
Reply With Quote
  #4 (permalink)  
Old August 9th, 2016, 01:45 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,145, Level: 23
Points: 3,145, Level: 23 Points: 3,145, Level: 23 Points: 3,145, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 641
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Convert is a class defined in the System namespace. It has a bunch of static methods including ToInt32.

If you don't use any using statements, you could do this:

Code:
myDirection = System.Convert.ToInt32(System.Console.ReadLine());
This is the "most primitive" form.

If you include the System namespace, then you can use the classes it defines without explicitly giving the namespace like this:

Code:
using System;
...
myDirection = Convert.ToInt32(Console.ReadLine());
This using directive works for both Convert and Console because they are both inside that namespace.

This is what I would normally do because it's easy to tell that ToInt32 comes from the Convert class and ReadLine comes from the Console class.

You can also add the static keyword to gain transparent access to the static methods of classes like this:

Code:
using static System.Console;
using static System.Convert;
...
myDirection = ToInt32(ReadLine());
This is certainly shorter, but I think more confusing. And the three seconds you save in typing is often offset by three hours of debugging later.

You shouldn't be able to do this:

Code:
using System.Convert;
If you don't use the static keyword, then Visual Studio tries to treat System.Convert as a namespace, but it's a class not a namespace. If you add the static keyword, then it knows that you're using a class and not a namespace.

Last comment: When you use "using static System.Convert;" that doesn't automatically include anything else in the System namespace, just the static methods in the Convert class.

In fact, it doesn't even let you do this:

Code:
myDirection = Convert.ToInt32(ReadLine());
If you don't include the System namespace, then it can't find the Convert class, even though it can find that class's static members.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #5 (permalink)  
Old August 9th, 2016, 02:00 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2016
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

Assume Sauce, now my thick skull is starting to understand. I guess the confusion is because Visual Basic (Dim - Declare) thoughts are still pending and need to be purged.

Thanks for you quick response.
Reply With Quote
  #6 (permalink)  
Old August 9th, 2016, 04:42 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,145, Level: 23
Points: 3,145, Level: 23 Points: 3,145, Level: 23 Points: 3,145, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 641
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Ah ha! I suspect Microsoft added the static option to the using directive because Visual Basic has code modules. You can refer to methods defined in a code module without needing to refer to any class, but all methods must be inside classes in C#. The "using static" syntax lets you sort of weasel around that so you can refer to some methods without mentioning the class.

So that's where I think this comes from.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
error CS0234: The type or namespace name 'Xml' does not exist in the namespace 'Syste shailesh_kumar C# 2008 aka C# 3.0 8 August 20th, 2009 03:11 AM
Need to convert an existing namespace and add new namespace to the SOAP xml Prabeen XSLT 10 April 28th, 2009 10:18 AM
namespace FT BOOK: ASP.NET Website Programming Problem-Design-Solution 3 December 4th, 2005 09:52 PM
Where Is NameSpace? reidcor BOOK: ASP.NET Website Programming Problem-Design-Solution 1 March 23rd, 2004 02:41 PM



All times are GMT -4. The time now is 08:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.