Oztaco
Oztaco

Reputation: 3459

Use a class inside another class?

I have a class in my program that I always have to access like this:

Blah.Blah.Blah.DoSomething();

And I want to be able to access DoSomething() without having to type all this out each time. using Blah; wouldn't compile, and wouldn't come up on Visual Studio's intellisense anyway. How can I accomplish this?

Upvotes: 2

Views: 99

Answers (2)

ChrisK
ChrisK

Reputation: 1218

You can use using directives only for Namespaces. You can think of namespaces like folders to organize classes. For instance, the Console class lives in the System namespace. So you can either access it with System.Console or with using System; ... Console.

You can use using directives only for namespaces, not for classes. In your example, the DoSomething is a method of the Blah class. Writing only DoSomething doesn't work, because the compiler wouldn't know which function you are looking for. Also sometimes large chains like in your example come up when you access properties of properties. Imagine, I have a Color class with a R, a G and a B value. Now I have a Pixel Class, that also stores an object of the Type Color. So to get the R value of such a Pixel, I could use Pixel.Color.R. In this case it's not possible to use usings, because neither Pixel nor Color is a namespace.

Edit: As Scott points out, you can indeed use using statement on classes. See below.

Upvotes: 1

Kinexus
Kinexus

Reputation: 12904

Try this;

 using blah = blah.blah.blah;

Upvotes: 6

Related Questions