Results 1 to 13 of 13

Thread: [RESOLVED] Use {} to limit scope?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Resolved [RESOLVED] Use {} to limit scope?

    Hi,

    Is it an accepted use of brackets if I do something like this inside a method?
    Code:
    string[] strs;
    {
        object SomethingHeavy = new HeavySomething();
        strs = HeavyToString(SomethingHeavy);
    }
    //use strs
    I figured I could use this way to get rid off the SomethingHeavy object in stead of using .Dispose() and I wouldn't have a reference to it anymore.

    I haven't seen it in other people's C# code so I was just wondering if it would look akward to pro's like you all are ofcourse.

    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Use {} to limit scope?

    Anything wrong with:
    Code:
    string[] strs = HeavyToString(new HeavySomething());
    ?
    Last edited by penagate; Nov 2nd, 2005 at 02:40 AM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Use {} to limit scope?

    Absolutely not but I have done it just a few times were a multitude of objects needed to be instanciated for a short time, I wanna know if it makes sence to people.

    Edit: bare in mind I'm a total amateur.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Use {} to limit scope?

    It was my understanding that objects were destroyed once they went out of scope, or at routine garbage collection, if there were no longer any references to them. If you use the one-liner, you don't actually create any references, so I'd take it to be the same as your code.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Use {} to limit scope?

    You should always use Dispose() if an object has it, its good practice as it makes it obvious what is going on. Use of {} to limit scope is perfectly OK. After all you di it after most of your If() statements anyway...

    If(whatever)
    {
    //blah
    }
    I don't live here any more.

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Use {} to limit scope?

    if you're talkign about disposing objects when the y go out of scope, then use the "using" statement
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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

    Re: Use {} to limit scope?

    Quote Originally Posted by MrPolite
    if you're talkign about disposing objects when the y go out of scope, then use the "using" statement
    I thought there was something like that but I couldn't find reference to it in the help. Does "using" only let you specify one object for the block or more?
    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

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Use {} to limit scope?

    MSDN has all the answers.
    I don't live here any more.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Wink Re: Use {} to limit scope?

    I'm sorry but lol. Pointing MSDN out to jmcilhinney....


    ..

    .
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

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

    Re: Use {} to limit scope?

    Quote Originally Posted by wossname
    MSDN has all the answers.
    So it does. Who would have thought? I figured that it would be no easier to find on MSDN online than via VS.NET help, but it seems I was wrong. I still can't find it in the help though, which seems strange, although I'm sure it must be there somewhere.
    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

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

    Re: Use {} to limit scope?

    Quote Originally Posted by grilkip
    I'm sorry but lol. Pointing MSDN out to jmcilhinney....


    ..

    .
    Ridicule me all you like but... hang on... no... stop ridiculing me... nooooooooooooo!
    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

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Use {} to limit scope?

    Quote Originally Posted by jmcilhinney
    Ridicule me all you like but... hang on... no... stop ridiculing me... nooooooooooooo!
    hehe n'ing bu' respec' here


    Thanks for the answers, I guess it is acceptable but not a common practice.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  13. #13
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Use {} to limit scope?

    haha I thought for a sec this was chitchat and I was like "jmc posts in chit chat too?"


    http://msdn.microsoft.com/library/de...pspec_8_13.asp
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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