Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

"global::" usage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
twang090@gmail.com - 22 Oct 2007 16:36 GMT
I find in other team member's project, they are referencing a type in
following format
"
           public static global::ErrorReport.Description Description
= new global::ErrorReport.Description();
"

I do not understand the usage of "global" here, could anyone please
clarify its usage and what benift of using that. Thanks in advance.
Nicholas Paldino [.NET/C# MVP] - 22 Oct 2007 16:49 GMT
In C#, you can have namespaces and classes which share a name as well as
have nested namespaces/classes which have are shared, causing ambiguity.

   Using "global::" indicates that the namespace after it will be processed
from the root level, not using the immediate namespace/class scope for
resolution.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I find in other team member's project, they are referencing a type in
> following format
[quoted text clipped - 5 lines]
> I do not understand the usage of "global" here, could anyone please
> clarify its usage and what benift of using that. Thanks in advance.
kentcb@internode.on.net - 22 Oct 2007 16:49 GMT
See http://msdn2.microsoft.com/en-us/library/c3ay4x3d(VS.80).aspx. You
will often see the global:: qualifier in generated code to ensure that
it is refering to the correct types. I don't think I've ever since it
used manually - it's rarely necessary. It's unclear from your example
whether it is actually needed or not. It depends whether there are
more than one ErrorReport types.

Regards,
Kent

On Oct 22, 4:36 pm, twang...@gmail.com wrote:
> I find in other team member's project, they are referencing a type in
> following format
[quoted text clipped - 5 lines]
> I do not understand the usage of "global" here, could anyone please
> clarify its usage and what benift of using that. Thanks in advance.
Stoitcho Goutsev (100) - 22 Oct 2007 18:41 GMT
In c# you cannot create a class that does not belong to a namespace, however
in some other languages such as IL and I blieve C++ and VB.NET you can. the
*global* scope has been added to C# 2.0 in order to solve the problems where
programmers have ran accross such assemblies containing types declared
outside any namespace.

Signature

HTH
Stoitcho Goutsev (100)

>I find in other team member's project, they are referencing a type in
> following format
[quoted text clipped - 5 lines]
> I do not understand the usage of "global" here, could anyone please
> clarify its usage and what benift of using that. Thanks in advance.
Jon Skeet [C# MVP] - 22 Oct 2007 18:55 GMT
> In c# you cannot create a class that does not belong to a namespace, however
> in some other languages such as IL and I blieve C++ and VB.NET you can. the
> *global* scope has been added to C# 2.0 in order to solve the problems where
> programmers have ran accross such assemblies containing types declared
> outside any namespace.

In C# you can create a class which doesn't have any declared namespace
- just don't use a namespace declaration. Is there a difference between
that and the kind of class you can create in C++ or VB.NET?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Peter Ritchie [C# MVP] - 22 Oct 2007 20:43 GMT
> Is there a difference between
> that and the kind of class you can create in C++ or VB.NET?

The VB language supports classes at the global scope; but the VB.NET IDE
does not; you must specific a default namespace for a project in which all
classes and namespaces will be nested.  I don't know what the global scope
resolution operator/keyword is in VB...

In C++/CLI you can also have managed classes at global scope.  In C++ the
global scope resolution operator is '::', the equivalent of 'global::'.  But,
often Intellisense gets confused with that...
Mattias Sjögren - 22 Oct 2007 21:30 GMT
>The VB language supports classes at the global scope; but the VB.NET IDE
>does not; you must specific a default namespace for a project in which all
>classes and namespaces will be nested.

You don't have to specify one, you can leave it blank.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Peter Ritchie [C# MVP] - 22 Oct 2007 21:37 GMT
I've had it complain that the root namespace can't be empty.  But, with some
projects it does let you have an empty namespace.  Interesting...

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> >The VB language supports classes at the global scope; but the VB.NET IDE
> >does not; you must specific a default namespace for a project in which all
[quoted text clipped - 3 lines]
>
> Mattias
Peter Ritchie [C# MVP] - 22 Oct 2007 21:38 GMT
It appears you can't have an empty root namespace for class library projects
in VB, but you can for Console/Winform applications.  Interesting...

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> >The VB language supports classes at the global scope; but the VB.NET IDE
> >does not; you must specific a default namespace for a project in which all
[quoted text clipped - 3 lines]
>
> Mattias
Peter Ritchie [C# MVP] - 22 Oct 2007 21:40 GMT
I'm not sure what's different, but I have some VB class library projects not
allow a blank root namespace and some do.  Interesting...

I'll add that to my list of reasons why I don't like VB...

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> >The VB language supports classes at the global scope; but the VB.NET IDE
> >does not; you must specific a default namespace for a project in which all
[quoted text clipped - 3 lines]
>
> Mattias
Nicholas Paldino [.NET/C# MVP] - 22 Oct 2007 18:58 GMT
Stoitcho,

   That's not true.  It was meant for the following example:

namespace Acme
{
   public class MyClass {}
}

namespace MySystem
{
   public class Acme
   {
       public class MyClass {}
   }

   public class MyOtherClass
   {
       public Acme.MyClass MyAcmeClass;
   }
}

   In the example above, the compiler would always resolve the MyAcmeClass
to the type MySystem.Acme.MyClass.  If you wanted it to be Acme.MyClass,
there was no way to do it until C# 2.0.  In C# 2.0, you would use:

   public class MyOtherClass
   {
       public global::Acme.MyClass MyAcmeClass;
   }

   To indicate that you shouldn't use the local namespace scope, but
rather, the root namespace.

   While the other cases might be true, the primary reason was for the
above case (I was there when the C# team presented this feature to a closed
customer council).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> In c# you cannot create a class that does not belong to a namespace,
> however in some other languages such as IL and I blieve C++ and VB.NET you
[quoted text clipped - 11 lines]
>> I do not understand the usage of "global" here, could anyone please
>> clarify its usage and what benift of using that. Thanks in advance.
Stoitcho Goutsev (100) - 23 Oct 2007 15:16 GMT
Thanks Nicholas and all others,

for the clarifications. Indeed my  statemement wasn't right.

Regards,
Stoitcho Goutsev (100)

> Stoitcho,
>
[quoted text clipped - 49 lines]
>>> I do not understand the usage of "global" here, could anyone please
>>> clarify its usage and what benift of using that. Thanks in advance.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.