Results 1 to 3 of 3

Thread: Procedure level import/using statement

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Procedure level import/using statement

    I'm wondering, is there any statement that imports a namespace on a procedural level? For example:
    Code:
    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:
    Code:
    System.Something.SomeClass sc = new System.Something.SomeClass();
    My.Something.SomeClass sc2 = new My.Something.SomeClass();

  2. #2
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Procedure level import/using statement

    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.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Procedure level import/using statement

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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