473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using statement and as

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
6 1940
"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
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
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
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
5
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope...
5
by: charliewest | last post by:
I've implemented the USING statement to ensure that my newly created sql connection closes when my method is finished using it. The USING statement is wrapped in try/catch error handling statement....
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
2
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel....
6
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am running a simple query using VB (This isnt a VB Question, dont worry!) on SQL Server Compact. I have the query below being created, and then added to if a flower location doesn't...
4
by: Jure Bogataj | last post by:
Hello! I'm using .NET 2005 (C#) and I've come across using statement. It seems ok, I was just wondering one thing. What if I returned object inside using statement using RETURN directive. Is...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
15
by: tshad | last post by:
Do I still need to close an object in a finally statement after a catch if it is part of a using statement? For example: FileStream fs = null; try { using (FileInfo fInfo = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.