p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Stephens' Visual Basic Programming 24-Hour Trainer (http://p2p.wrox.com/book-stephens-visual-basic-programming-24-hour-trainer-621/)
-   -   namespace query (http://p2p.wrox.com/book-stephens-visual-basic-programming-24-hour-trainer/99128-namespace-query.html)

Robguay August 9th, 2016 12:37 PM

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

Rod Stephens August 9th, 2016 01:06 PM

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.

Robguay August 9th, 2016 01:28 PM

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

Rod Stephens August 9th, 2016 01:45 PM

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.

Robguay August 9th, 2016 02:00 PM

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.

Rod Stephens August 9th, 2016 04:42 PM

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.


All times are GMT -4. The time now is 04:08 PM.

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