Click to See Complete Forum and Search --> : StreamReader problem


zhujp98
July 16th, 2005, 03:19 PM
I have .net 2003
in my code
if I use
StreamReader dataInput =new StreamReader(c0Name), I will get error

Type or namespace streamreader could not be found

if i use
System.IO.StreamReader dataInput =new System.IO.StreamReader(c0Name)

It works,
I remember, i my previous project i do need to include the System.IO in my code.

THX

darwen
July 16th, 2005, 04:39 PM
Did you have the following at the top of your code :


using System.IO;


This statement effectively defines which namespaces the file is "using" - i.e. you don't need to include fully qualified names for each class in that particular namespace.

Darwen.

K Nemelka
July 18th, 2005, 04:57 PM
if i use
System.IO.StreamReader dataInput =new System.IO.StreamReader(c0Name)

It works,
I remember, i my previous project i do need to include the System.IO in my code.

THX

I would sure like to know how you can use the StreamReader class without including System.IO. I didn't think it could be done...

darwen
July 18th, 2005, 06:57 PM
Sorry, I just skim read the post.

Yes, you can give the fully qualified names of any class: so long as it's included in the "references" of the project.

So


System.IO.StreamReader = new System.IO.StreamReader...


is fine without the "using".

"using" is a shorthand way of addressing a namespace.

i.e. if you have "using System.IO" the compiler knows which namespaces to use for a particular .cs file, and can resolve it down to "System.IO.StreamReader".

The downside of "using" is that you can have conflicts : the Timer class is a good example of this.

There exists a Timer class in System.Windows.Forms as well as System.Threading and System.Timers.

That's one class with the same name, but in different namespaces and very different implementations.

I hope I'm not confusing you here... basically each class has it's own name, which includes which namespace it's in.

"using" at the top of a .cs file just tells the compiler which namespaces to search for any particular class name.

If there is a conflict, then the C# compiler will tell you.

The keyword "using" in this sense is used at the top of the cs file : you can use it in code blocks too which is detailed in my article :

http://www.codeguru.com/Csharp/Csharp/cs_syntax/interfaces/article.php/c8679/

Darwen.