469,275 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

The :: operator, and "global"

I saw in a recent post the :: operator used to reach the global namespace, as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there any
other use for these operators, than as a pair?

TIA,

Javaman
Nov 17 '05 #1
9 2243
Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator '::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio
"Javaman59" <Ja*******@discussions.microsoft.com> escribió en el mensaje
news:76**********************************@microsof t.com...
I saw in a recent post the :: operator used to reach the global namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there
any
other use for these operators, than as a pair?

TIA,

Javaman

Nov 17 '05 #2
You should probably check before you post. Your code will not compile - thats a C++ construct. There is a global keyword in C# v2 and it always goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo" );

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator '::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio
"Javaman59" <Ja*******@discussions.microsoft.com> escribi? en el mensaje
news:76**********************************@microsof t.com...
I saw in a recent post the :: operator used to reach the global namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there
any
other use for these operators, than as a pair?

TIA,

Javaman


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #3
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always considered
excellent:

http://msdn.microsoft.com/msdnmag/is...0/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> escribió en
el mensaje news:%2****************@TK2MSFTNGP09.phx.gbl...
You should probably check before you post. Your code will not compile -
thats a C++ construct. There is a global keyword in C# v2 and it always
goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo" );

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
'::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio
"Javaman59" <Ja*******@discussions.microsoft.com> escribi? en el mensaje
news:76**********************************@microsof t.com...
I saw in a recent post the :: operator used to reach the global
namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is
there
any
other use for these operators, than as a pair?

TIA,

Javaman


[microsoft.public.dotnet.languages.csharp]

Nov 17 '05 #4
Javaman59 wrote:
I saw in a recent post the :: operator used to reach the global namespace, as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there any
other use for these operators, than as a pair?


Apart from the other replies, there's another, maybe more common, use
for the :: operator. You know the way you can define aliases in a using
statement? Like this:

using SWF = System.Windows.Forms;

Now you can use SWF as a shortcut for the System.Windows.Forms
namespace. In .NET 1.1, you used to write

SWF.Form

or whatever to refer to a type within the aliased namespace. This could
obviously be an ambiguous expression in certain circumstances, so in
..NET 2, the :: operator was introduced to be used in this situation. So
in .NET 2 you'd write

SWF::Form

instead, which is no longer ambiguous. The "global" alias is nothing
more than a default alias for the topmost namespace and can't be used in
any other context, as the other posters already said.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #5
Thanks Octavio for answering my question. I had a quick look at the article
too, and it does say the "::" is valid as a prefix (ie. your code should
compile). Maybe someone was hoping that c++ style "::" would return, but
Microsoft eventually decided against it :). I'm glad, too, because I always
found that dangling "::" confusing - "global::" is much cleareer!

- Javaman

"Octavio Hernandez" wrote:
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always considered
excellent:

http://msdn.microsoft.com/msdnmag/is...0/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> escribió en
el mensaje news:%2****************@TK2MSFTNGP09.phx.gbl...
You should probably check before you post. Your code will not compile -
thats a C++ construct. There is a global keyword in C# v2 and it always
goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo" );

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
'::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio
"Javaman59" <Ja*******@discussions.microsoft.com> escribi? en el mensaje
news:76**********************************@microsof t.com...
I saw in a recent post the :: operator used to reach the global
namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is
there
any
other use for these operators, than as a pair?

TIA,

Javaman


[microsoft.public.dotnet.languages.csharp]


Nov 17 '05 #6
Thanks Oliver,

That's really what I was looking for. MSDN on the "::" operator says that it
is placed between two identifiers, but it isn't clear how it can be used,
apart from "global::SomeNamespace". Your reply clears it up for me.

- Javaman

"Oliver Sturm" wrote:
Javaman59 wrote:
I saw in a recent post the :: operator used to reach the global namespace, as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there any
other use for these operators, than as a pair?


Apart from the other replies, there's another, maybe more common, use
for the :: operator. You know the way you can define aliases in a using
statement? Like this:

using SWF = System.Windows.Forms;

Now you can use SWF as a shortcut for the System.Windows.Forms
namespace. In .NET 1.1, you used to write

SWF.Form

or whatever to refer to a type within the aliased namespace. This could
obviously be an ambiguous expression in certain circumstances, so in
..NET 2, the :: operator was introduced to be used in this situation. So
in .NET 2 you'd write

SWF::Form

instead, which is no longer ambiguous. The "global" alias is nothing
more than a default alias for the topmost namespace and can't be used in
any other context, as the other posters already said.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #7
One final (minor) point....
MSDN on the "::" operator says that it
is placed between two identifiers


Note that is says "identifier" and not "keyword".

Technically, global is not a keyword (neither are get, set, value, partial,
alias, add, remove, where, yield).
Nov 17 '05 #8
Javaman,

Researching on this topic I've found that there is already an official
standard for C# 2.0:

http://www.ecma-international.org/pu...s/Ecma-334.htm

Of course it fully describes the usage of the '::' operator and the 'global'
identifier.

Thanks and best regards - Octavio

"Javaman59" <Ja*******@discussions.microsoft.com> escribió en el mensaje
news:0B**********************************@microsof t.com...
Thanks Octavio for answering my question. I had a quick look at the
article
too, and it does say the "::" is valid as a prefix (ie. your code should
compile). Maybe someone was hoping that c++ style "::" would return, but
Microsoft eventually decided against it :). I'm glad, too, because I
always
found that dangling "::" confusing - "global::" is much cleareer!

- Javaman

"Octavio Hernandez" wrote:
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always
considered
excellent:

http://msdn.microsoft.com/msdnmag/is...0/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> escribió
en
el mensaje news:%2****************@TK2MSFTNGP09.phx.gbl...
> You should probably check before you post. Your code will not compile -
> thats a C++ construct. There is a global keyword in C# v2 and it always
> goes together with :: to indicate global scope of the type.
>
> global::System.Windows.Forms.MessageBox.Show("foo" );
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>
> Javaman,
>
> AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
> '::'
> to indicate the compiler to start the search from the 'outest' scope:
>
> namespace A {
> namespace System {
> class X {
> public void Message(string s)
> {
> ::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
> }
> }
> }
> }
>
> Regards - Octavio
>
>
> "Javaman59" <Ja*******@discussions.microsoft.com> escribi? en el
> mensaje
> news:76**********************************@microsof t.com...
> >I saw in a recent post the :: operator used to reach the global
> >namespace,
> >as
> > in
> >
> > global::MyNamespace
> >
> > I hadn't seen this before, so looked it up in MSDN, which explained
> > it
> > nicely. My question is, do "global" and "::" always go together? Is
> > there
> > any
> > other use for these operators, than as a pair?
> >
> > TIA,
> >
> > Javaman
>
>
>
> [microsoft.public.dotnet.languages.csharp]


Nov 17 '05 #9
Thanks Mark. That is actually very helpful, and worth remembering. Now I know
why I couldn't find "global keyword" in the help index.

- Javaman

"MarkT [developmentor]" wrote:
One final (minor) point....
MSDN on the "::" operator says that it
is placed between two identifiers


Note that is says "identifier" and not "keyword".

Technically, global is not a keyword (neither are get, set, value, partial,
alias, add, remove, where, yield).

Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by mrbog | last post: by
1 post views Thread by Chris Stromberger | last post: by
2 posts views Thread by Steve | last post: by
7 posts views Thread by twang090 | last post: by
4 posts views Thread by =?Utf-8?B?QWxleCBNdW5r?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.