Can't use using in method???

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

This isn't legal???

class Something
{
public void OrOther()
{
using System; // error!
}
}

From what I can tell from the docs, using is only usable at the global level or
within an enclosing namespace, but not within a method --

If that is truly the case, then I put using on my short list of directives to
*not* use...
 
keyword "using" is for defining namespace to the whole file.
even you have lots of classes in a file, you also need one on the top
if you feel import everything is too bad, the you have use System.XXX.xxx to
create your instances.
 
Julie:

No, it's not legal the way you are using it (pardon the pun). using at the
top of a class does "importing" of namespaces for you. If you use it inside
a class it takes care of disposing of the object when you are done with it:

This isn't ok at the topof a module:

using SqlDataReader;

however, inside a method this is legal:

using(SqlConnection = new SqlConnection(ConnectString)){//whatever}

using System.Data.SqlClient; //totally ok at the top of a class.

So using has two different uses.

HTH,

Bill
 
Julie said:
This isn't legal???

class Something
{
public void OrOther()
{
using System; // error!
}
}

From what I can tell from the docs, using is only usable at the global level or
within an enclosing namespace, but not within a method --

If that is truly the case, then I put using on my short list of directives to
*not* use...

Huh? Why do you want to use it inside a method? Just curious :-)
 
Julie said:
This isn't legal???

class Something
{
public void OrOther()
{
using System; // error!
}
}

From what I can tell from the docs, using is only usable at the global
level or
within an enclosing namespace, but not within a method --
I have to echo the choir and ask why you would want to do this. This
particular syntax looks like its asking for impossible to understand code.
The meaning of a type really shouldn't change from one method to the next,
IMHO, which allowing using at any level outside of global or immediatly
inside a namespace allows for.
 
Julie said:
This isn't legal???
Nope.

class Something
{
public void OrOther()
{
using System; // error!
}
}

From what I can tell from the docs, using is only usable at the
global level or within an enclosing namespace, but not within a
method --
Indeed.

If that is truly the case, then I put using on my short list of
directives to
*not* use...

Why, exactly?
 
Jon Skeet said:
Why, exactly?

I'm finding it hard to justify -- essentially it renders namespaces useless to
just strip away the namespace w/ a using statement.

In C++, you can use a using within function scope, that is why I was asking. I
had never used it, but I'm finding all sorts of problems w/ the global using
directive and name clashes due to the stripping of the namespace.

I'll probably just stop 'using' all together, seems that it would make for
longer names, but explicitly more clear code.
 
pei_world said:
keyword "using" is for defining namespace to the whole file.
even you have lots of classes in a file, you also need one on the top
if you feel import everything is too bad, the you have use System.XXX.xxx to
create your instances.

Yes, I do think that it is bad and leads to future maintenance problems by
stripping the namespace away. I'm going to try using fully-qualified names and
see what I think.
 
C# Learner said:
Huh? Why do you want to use it inside a method? Just curious :-)

That way you could use the effects of using within that method (or block for
that matter), and then have it revert to default (fully-qualified namespace) on
scope exit.

This is possible under C++, although I've never used it.

Right now, I'm leaning to not using 'using' at all, and just fully-qualifying
all names.
 
Daniel O'Connell said:
I have to echo the choir and ask why you would want to do this. This
particular syntax looks like its asking for impossible to understand code.
The meaning of a type really shouldn't change from one method to the next,
IMHO, which allowing using at any level outside of global or immediatly
inside a namespace allows for.

It is possible in C++, although I've never used it. I hear your argument about
the same name potentially meaning different things in different methods (or
blocks) depending on the using. However, that exact problem exists between
files.

I guess that it all comes down to namespace naming theory, something that I
probably need to research more of.
 
Julie said:
It is possible in C++, although I've never used it. I hear your argument
about
the same name potentially meaning different things in different methods
(or
blocks) depending on the using. However, that exact problem exists
between
files.

When you change a file in an IDE, you are doing considerable work and it is
very apparent that the context is changing. When the same identifier can
mean very different things across 10 lines, there is no seperator that keeps
things simple.

I really don't see why you feel using at the method level will help you.
While there are issues with name collisions, they are really rather rare. I
can't say I've run into them more than 4 or 5 times in the last couple of
years(They are annoying as hell when they come about, but they are the
unusual case). Changing all your code to use fully qualified namespaces
would result in harder to read code simply to avoid those edge cases.
 
Julie said:
That way you could use the effects of using within that method (or block for
that matter), and then have it revert to default (fully-qualified namespace) on
scope exit.

This is possible under C++, although I've never used it.

Right now, I'm leaning to not using 'using' at all, and just fully-qualifying
all names.

Ah, so you want to use it for readability purposes, right?

i.e. so code can be read as: "In this method I'm using namespace X.Y, so
any unqualified names that don't look familiar will probably belong in
that namespace."?
 
Julie said:
I'm finding it hard to justify -- essentially it renders namespaces useless to
just strip away the namespace w/ a using statement.

It doesn't render them useless at all. Namespaces prevent name
collision. If you have two classes with the same name in different
namespaces, you can always disambiguate by using the full name, which
you couldn't do without namespaces. The "using" statement just allows
you to avoid typing the full name when there *aren't* any collisions.
In C++, you can use a using within function scope, that is why I was asking. I
had never used it, but I'm finding all sorts of problems w/ the global using
directive and name clashes due to the stripping of the namespace.

What sort of problems? How often is this occurring? I ask because I
only run into it once in a blue moon...
I'll probably just stop 'using' all together, seems that it would make for
longer names, but explicitly more clear code.

I think you'll find you either change your mind quickly, or if you're
working with others they'll petition you to change your mind. There's a
good reason why basically no-one does this.
 
To be a programmer, you must first adhere to the syntax of the language.
In this context, 'using' cannot be used within a method but in other
contexts, using can be when you wish to declare an object for
deterministic collection.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top