using statement and as

  • Thread starter Thread starter Juan Zamudio
  • Start date Start date
J

Juan Zamudio

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
 
Juan Zamudio said:
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
 
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.
 
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
 
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
 
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
 
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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top