473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2461
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
2
by: Steve | last post by:
I am new to this newsgroup & to .NET in general. I have been playing around with Visual Studio .NET, building and rendering web pages using VB "code behind" files. My problem / question is; How...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
7
by: twang090 | last post by:
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...
4
by: =?Utf-8?B?QWxleCBNdW5r?= | last post by:
My Web application is developed in C# Visual Studio 2005 Professional. After deploying the application to the production server I am getting the following error: <%@ Application...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.