PDA

Click to See Complete Forum and Search --> : Procedure level import/using statement


gavio
Aug 21st, 2011, 04:21 AM
I'm wondering, is there any statement that imports a namespace on a procedural level? For example:void Test()
{
using System.Something;

SomeClass sc = new SomeClass();

...
}If there's not, should it be? Why it shouldn't be?

It bothers me that I have to watch and avoid general names, cause I like general names, like for example Image and so on (cause it already exists in System.Drawing). Then, I cannot use the two in the same class withouth doing this:System.Something.SomeClass sc = new System.Something.SomeClass();
My.Something.SomeClass sc2 = new My.Something.SomeClass();

ntg
Aug 31st, 2011, 01:49 PM
I don't think that you can use a Using statement at a method level. And even if you could, if a method accessed both System.Something.SomeClass and My.Something.SomeClass you'd still have the same problem.

Naming problems such as these have been a problem for several people in the past. Other than avoiding creating custom classes that are named after common .Net classes, I don't think there's a lot you can do about it.

jmcilhinney
Sep 7th, 2011, 12:20 AM
If you really want to use the same type name unqualified to refer to two different types in the same class then you could create two partial classes. Import one namespace in one file and include all the members that use that type and import the other namespace in the other file. Sounds like a great way to cause confusion though. I don't know anyone for whom this type of name clash happens very often so, if it does for you, I'd suggest that you're just not very good at choosing names.