By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

using statement and as

P: n/a
Hi all, I'm using vs 2003, i have a question about using and as, i was
reading in programing .net components that one way to avoid null
references inside using was using as, something like this:
using(var as IDisposable){
.....
}
the book saids that if the conversion is null the code inside using will
not run, but i cant make it work, I have a FileStream that opens a file,
the path is a parameter, something like this:

using((fileStream=new FileStream((fullPath,FileMode.Open,
FileAccess.Read,FileShare.Read)) as IDisposable){
.....
}

But if the path points to a non existing file the code inside the using
throws an exception, the only way to avoid this is to check for null
before using???, thanks a lot.

Juan Zamudio
Jun 5 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Juan Zamudio" <jz*************@cnbv.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi all, I'm using vs 2003, i have a question about using and as, i was
reading in programing .net components that one way to avoid null
references inside using was using as, something like this:
using(var as IDisposable){
....
}
the book saids that if the conversion is null the code inside using will
not run, but i cant make it work, I have a FileStream that opens a file,
the path is a parameter, something like this:

using((fileStream=new FileStream((fullPath,FileMode.Open,
FileAccess.Read,FileShare.Read)) as IDisposable){
....
}

But if the path points to a non existing file the code inside the using
throws an exception, the only way to avoid this is to check for null
before using???, thanks a lot.
The article said the code will not run if the result is null, it said
nothing about suppressing exceptions creating said object. You're trying to
cram too much into a single line of code. Use the proper try catch
statements.

Michael
Jun 5 '07 #2

P: n/a
Juan,

The exception you are getting is not because of the as statement, but
because of the constructor for the FileStream class. You need to ensure
that returns correctly before the as operator is evaluated.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Juan Zamudio" <jz*************@cnbv.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi all, I'm using vs 2003, i have a question about using and as, i was
reading in programing .net components that one way to avoid null
references inside using was using as, something like this:
using(var as IDisposable){
....
}
the book saids that if the conversion is null the code inside using will
not run, but i cant make it work, I have a FileStream that opens a file,
the path is a parameter, something like this:

using((fileStream=new FileStream((fullPath,FileMode.Open,
FileAccess.Read,FileShare.Read)) as IDisposable){
....
}

But if the path points to a non existing file the code inside the using
throws an exception, the only way to avoid this is to check for null
before using???, thanks a lot.

Juan Zamudio
Jun 5 '07 #3

P: n/a
The article said the code will not run if the result is null, it said
nothing about suppressing exceptions creating said object.
Exactly, the example wasn't clear enough, and english is not my first
language so forgive me. I tried to minimize the code but then i
realize the example wasn't clear enough.
In my original code i have a function that returns null if the file is
not found, like this:
//GetFileStream returns null if the file does not exist
using((fileStream=GetFileStream())) as IDisposable){
.....
}

but the code inside the using stills run, even if i do this:
fileStream=GetFileStream();
using(fileStream as IDisposable){
.....
}
the code inside using always runs, always, thanks for your help.

Juan Zamudio

Jun 5 '07 #4

P: n/a
On Jun 5, 4:00 pm, jmzl666 <jmzl...@gmail.comwrote:

<snip>
the code inside using always runs, always, thanks for your help.
Yes, the code inside the using statement will always run. If the
expression used for the using statement is null, Dispose() won't be
called on it, but the statement's body will still execute.

What *exactly* does your book say?

Jon

Jun 5 '07 #5

P: n/a
>
What *exactly* does your book say?

Jon
Thanks Jon i guess in the end i didn't understand quite well the
message in the book, thanks a lot for your help.
Juan Zamudio


Jun 5 '07 #6

P: n/a
On Jun 5, 9:23 pm, jmzl666 <jmzl...@gmail.comwrote:
What *exactly* does your book say?
Jon

Thanks Jon i guess in the end i didn't understand quite well the
message in the book, thanks a lot for your help.

Juan Zamudio
Hmm. Never used 'using' with 'as' as mentioned above. For stream
objects 'using' statement would be ideal for instance creation,
considering object scope. And 'as' operator for checking type with
null - after we do 'object as Type'.

Jun 5 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.