user1202606
user1202606

Reputation: 1160

system.drawing reference

I'm trying to set the backColor of a text box like this:

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;

It doesn't like it because it wants me to add System in front like:

txtCompanyName.BackColor = System.Drawing.Color.WhiteSmoke;

Now that works, but it irritates me that I have to type System. I'm referencing System at the top of my code with using System; Shouldn't that do the trick so I don't have to type System in front of drawing, not sure why I still have to type System, anybody know?

Upvotes: 3

Views: 1103

Answers (4)

Dave Cousineau
Dave Cousineau

Reputation: 13198

The using statement is importing the types that are in the specified namespace; this does not include child namespaces.

If you really wanted, you could use the following line:

using Drawing = System.Drawing;

which would allow you to refer to the System.Drawing namespace as 'Drawing'. This is probably not the best solution though. Really you should just use:

using System.Drawing;

your line then becomes:

txtCompanyName.BackColor = Color.WhiteSmoke;

if you need to disambiguate between System.Drawing.Color and some other Color class (like Microsoft.Xna.Framework.Color) you can use lines like this:

using XNAColor = Microsoft.Xna.Framework.Color;
using WinColor = System.Drawing.Color;

then your line is:

txtCompanyName.BackColor = WinColor.WhiteSmoke;

Upvotes: 1

JaredPar
JaredPar

Reputation: 755141

In C# you can't specify a type name by means of a partial namespace. In C# the name must either be

  • A fully qualified name including the entire namespace or namespace alias
  • A type name only

The Drawing portion of Drawing.Color.WhiteSmoke is a non-fully qualified namespace and hence illegal as a type name. You either need to add the prefix System or add a using System.Drawing and change the type name to Color.WhiteSmoke

Alternatively you can create an alias for the System.Drawing namespace named Drawing.

using Drawing = System.Drawing;

It is legal to use the alias as the start of a type name in C#.

Upvotes: 5

Roman Starkov
Roman Starkov

Reputation: 61502

Easily fixed:

using Drawing = System.Drawing;

...

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;

Upvotes: 1

yoozer8
yoozer8

Reputation: 7489

You're using system, but you aren't using System.Drawing. Add using System.Drawing; to the rest of your using statements.

Upvotes: 0

Related Questions