473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using = <type> directive

Can someone confirm that a) is illegal, and b) the solution?

a)

namespace Test
{
struct MyStruct
{
int a;
int b;
}
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

class MyClass
{
MyQueue q = new MyQueue();
}
}

b)

namespace Test
{
struct MyStruct
{
int a;
int b;
}
}

namespace Test
{
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

class MyClass
{
MyQueue q = new MyQueue();
}
}

If I'm right, anyone else agree that this seems just a little silly?
Dec 10 '07 #1
3 1492
Well the compiler is verifying this for you. What don't you believe
about the compiler. It's error message for a) is very specific about what
the issue is, and how to resolve it.

As for it being silly, no, I don't think so, since I think it would be
confusing to be able to have those defined arbitrarily all over the place.
It would make the code difficult to follow, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dylan Nicholson" <wi******@hotmail.comwrote in message
news:24**********************************@d27g2000 prf.googlegroups.com...
Can someone confirm that a) is illegal, and b) the solution?

a)

namespace Test
{
struct MyStruct
{
int a;
int b;
}
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

class MyClass
{
MyQueue q = new MyQueue();
}
}

b)

namespace Test
{
struct MyStruct
{
int a;
int b;
}
}

namespace Test
{
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

class MyClass
{
MyQueue q = new MyQueue();
}
}

If I'm right, anyone else agree that this seems just a little silly?
Dec 10 '07 #2
On Mon, 10 Dec 2007 14:41:35 -0800, Dylan Nicholson <wi******@hotmail.com
wrote:
Can someone confirm that a) is illegal, and b) the solution?
Well, you could have used, instead of b), this:

namespace Test
{
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

struct MyStruct
{
int a;
int b;
}

class MyClass
{
MyQueue q = new MyQueue();
}
}
[...]
If I'm right, anyone else agree that this seems just a little silly?
The compiler simply requires that the "using" directive come first. It's
about the only "pre-processor"-like thing C# supports, and I wouldn't use
the word "silly" to describe the requirement that the directive come
before any code the compiler will be compiling.

YMMV.

Pete

Dec 10 '07 #3
On Dec 11, 10:41 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 10 Dec 2007 14:41:35 -0800, Dylan Nicholson <wizof...@hotmail.com>
wrote:
Can someone confirm that a) is illegal, and b) the solution?

Well, you could have used, instead of b), this:

namespace Test
{
using MyQueue = System.Collections.Generic.Queue<MyStruct>;

struct MyStruct
{
int a;
int b;
}

class MyClass
{
MyQueue q = new MyQueue();
}

}

Ah, well, I thought I'd tried that, and because it put a squiggly line
under "MyStruct" in the using directive when I typed it, I assumed it
wasn't legal. But you're right, this is fine, and not really all that
silly.

Dec 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10929
by: Steve Wilkinson | last post by:
I've just started using managed C++ with VS2005, so please forgive my ignorance. I'm investigating producing a managed wrapper for some functionality of the Windows Media Format SDK. I have the...
8
5662
by: Rainer Queck | last post by:
Hi NG, I have a base class "Telegram". This class , as well as all descants have a static public field "public static int TelegramNo = <a tlg no>;" Now I added all descants of Telegram to a...
1
10206
by: lovecreatesbea... | last post by:
In the following function `HardwareStatusDb()', If its arguments are declared as type of `string', I get: main.cpp:5: error: expected initializer before ‘int’. The error disappears when those...
5
11139
by: Ronald S. Cook | last post by:
From my business tier (class) I get back an IQueryable<Penof data. Here is my client code that works fine: PenClass penClass = new PenClass(); IQueryable<Penpens = penClass.SelectPens(); ...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
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,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.