CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Location
    Georgia, USA
    Posts
    200

    StreamReader problem

    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

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: StreamReader problem

    Did you have the following at the top of your code :

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jul 2005
    Location
    Alpine, UT
    Posts
    26

    Re: StreamReader problem

    Quote Originally Posted by zhujp98

    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...

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: StreamReader problem

    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

    Code:
    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/Cshar...cle.php/c8679/

    Darwen.
    Last edited by darwen; July 18th, 2005 at 07:08 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured