|
-
Nov 2nd, 2005, 02:22 AM
#1
Thread Starter
Fanatic Member
[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
-
Nov 2nd, 2005, 02:35 AM
#2
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.
-
Nov 2nd, 2005, 02:40 AM
#3
Thread Starter
Fanatic Member
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
-
Nov 2nd, 2005, 02:51 AM
#4
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.
-
Nov 2nd, 2005, 05:00 AM
#5
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.
-
Nov 2nd, 2005, 02:43 PM
#6
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!!
-
Nov 2nd, 2005, 05:18 PM
#7
Re: Use {} to limit scope?
 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?
-
Nov 3rd, 2005, 05:44 AM
#8
Re: Use {} to limit scope?
MSDN has all the answers.
I don't live here any more.
-
Nov 3rd, 2005, 05:49 AM
#9
Thread Starter
Fanatic Member
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
-
Nov 3rd, 2005, 05:53 AM
#10
Re: Use {} to limit scope?
 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.
-
Nov 3rd, 2005, 05:54 AM
#11
Re: Use {} to limit scope?
 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!
-
Nov 3rd, 2005, 06:06 AM
#12
Thread Starter
Fanatic Member
Re: Use {} to limit scope?
 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
-
Nov 3rd, 2005, 04:22 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|