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((fileStre am=new FileStream((ful lPath,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 6 1956
"Juan Zamudio" <jz************ *@cnbv.comwrote in message
news:%2******** ********@TK2MSF TNGP05.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((fileStre am=new FileStream((ful lPath,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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Juan Zamudio" <jz************ *@cnbv.comwrote in message
news:%2******** ********@TK2MSF TNGP05.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((fileStre am=new FileStream((ful lPath,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
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((fileStre am=GetFileStrea m())) as IDisposable){
.....
}
but the code inside the using stills run, even if i do this:
fileStream=GetF ileStream();
using(fileStrea m as IDisposable){
.....
}
the code inside using always runs, always, thanks for your help.
Juan Zamudio
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
>
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
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'. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 .="<li>".$wohdate."</li>" ;
$TemplateText = Replace($TemplateText,"@$wohdisplayndx@",$woh_display);
|
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 define the servername / hostname in
my Perl Progrem..
Here is the code:
|
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 applies to objects created in the using scope.
For example:
<code>
static DataTable getTable()
|
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. This works fine.
When i try to implement the "SqlTransation" class, the code does not work as
the SqlTransation object is out-of-scope in the catch statement, and/or,
after the USING statement ends, the sql connection object is automatically...
|
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 (SqlCommand cm = new SqlCommand("ItemCount", cn)) {
cm.CommandType = CommandType.StoredProcedure;
| |
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.
Now a colege of mine suggested useinf the usingstatement, so that the office
object is dispossed/destroyued correctly, saying that it prevents
memoryleaks. Problem is I am having problems trying to achieve this, I keep
getting the error:
type...
|
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 equal nothing (if it is nothing, its returning all the values) now, when this query runs and I select a flower location, i.e. L3 I want it to return all values where the flower location is equal to L3 and those where it is equal to L5.
statement...
|
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 returned object valid or not?
For example:
public FileStream Func1()
{
|
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 scripting
language that makes writing automated tests simpler. Really, I'm
looking to kind of abstract the power of the C# language into a
simpler language that's easier to learn. The script files would be
interpreted by a script interpreter...
|
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 FileInfo(fromImagePath + "\\" +
(string)dr))
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |