I'm getting frustrated with using try...catch with local variables:
The code below wont compile in .NET 1.1: I get the following error: "Use of
unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
<code>
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
</code>
--
Thanks
Rick Hodder 28 3839
StreamReader oProcessFileRea der=null;
Now it's not unassigned.
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"RickHodder " wrote:
I'm getting frustrated with using try...catch with local variables:
The code below wont compile in .NET 1.1: I get the following error: "Use of
unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
<code>
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
</code>
--
Thanks
Rick Hodder
I'm getting frustrated with using try...catch with local variables:
>
The code below wont compile in .NET 1.1: I get the following error:
"Use of unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
Set oProcessFileRea der to null in your catch block.
Best Regards,
Dustin Campbell
Developer Express Inc.
Thanks Peter!
BTW, I really enjoy your blog.
--
Rick Hodder
"Peter Bromberg [C# MVP]" wrote:
StreamReader oProcessFileRea der=null;
Now it's not unassigned.
Peter
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"RickHodder " wrote:
I'm getting frustrated with using try...catch with local variables:
The code below wont compile in .NET 1.1: I get the following error: "Use of
unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
<code>
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
</code>
--
Thanks
Rick Hodder
"RickHodder " <Ri********@dis cussions.micros oft.comwrote in message
news:F0******** *************** ***********@mic rosoft.com...
I'm getting frustrated with using try...catch with local variables:
The code below wont compile in .NET 1.1: I get the following error: "Use
of
unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
<code>
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
</code>
--
Thanks
Rick Hodder
By just looking at the code and error, I see you are in fact not assigning
oProcessFileRea der before using it. Even though you are checking for null,
the compiler isn't smart enough to notice and it results in the error you
are getting. What you can do is simply modify your declaration to read:
StreamReader oProcessFileRea der = null;
and it should compile nicely (unless there are other errors in your code
that I'm over-looking.
Note: This is simply a compiler error and I'm not sure if you can turn off
this functionality. It exists to prevent you from doing something more
like:
StreamReader reader;
reader.ReadLine ();
Which is obviously bad, so the compiler warns/throws a tantrum when you try
to do something that even remotely resembles this...
HTH,
Mythran
Mythran wrote:
....
Note: This is simply a compiler error and I'm not sure if you can turn
off this functionality. It exists to prevent you from doing something
more like:
StreamReader reader;
reader.ReadLine ();
Which is obviously bad, so the compiler warns/throws a tantrum when you
try to do something that even remotely resembles this...
Do you mean that |reader| in your example is null? I am not sure
if memory for local variable declarations is primed with zeros. Is it
somewhere in the language specifications?
I am not sure if memory for local variable
declarations is primed with zeros. Is it
somewhere in the language specifications?
ECMA-334, §12 "A variable shall be definitely assigned (§12.3) before its
value can be obtained."
So within the language, it is irrelevant (an implementation detail) whether
the variable is zero'd - as you cannot legally look to see. The CLR may
behave differently, but since this is a C# NG...
Marc
Marc Gravell wrote:
>I am not sure if memory for local variable declarations is primed with zeros. Is it somewhere in the language specifications?
ECMA-334, §12 "A variable shall be definitely assigned (§12.3) before its
value can be obtained."
So within the language, it is irrelevant (an implementation detail) whether
the variable is zero'd - as you cannot legally look to see. The CLR may
behave differently, but since this is a C# NG...
Then, this is not a compiler error, contrary to what was suggested. Thanks.
Another way to write your code would be to use the "using" statement. It is
basically under the hood a try/finally statement which calls the Dispose
method on an object that implement IDisposable. If you want your catch
statement you will have to explicitly put that inside the using statment. So
your code:
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
would become (without the catch statement):
private void Test(string sFileName)
{
using(StreamRea der oProcessFileRea der = File.OpenText(s FileName))
{
//use the oProcessFileRea der
}
//here the stream has been disposed.
}
So even if an exception is thrown the resources are cleaned up, like in your
code, but with less typing :-).
Mark.
-- http://www.markdawson.org
"RickHodder " wrote:
I'm getting frustrated with using try...catch with local variables:
The code below wont compile in .NET 1.1: I get the following error: "Use of
unassigned local variable 'oProcessFileRe ader' "
Is there a way around this error?
<code>
private void Test(string sFileName)
{
StreamReader oProcessFileRea der;
try
{
oProcessFileRea der = File.OpenText(s FileName);
}
catch (Exception e)
{
MessageBox.Show ("Error: "+e.Message );
}
finally
{
if(oProcessFile Reader!=null)
oProcessFileRea der.Close();
}
}
</code>
--
Thanks
Rick Hodder
"Sericinus hunter" <se*****@flash. netwrote in message
news:#T******** ******@TK2MSFTN GP02.phx.gbl...
Marc Gravell wrote:
>>I am not sure if memory for local variable declaration s is primed with zeros. Is it somewhere in the language specifications?
ECMA-334, §12 "A variable shall be definitely assigned (§12.3) before its value can be obtained."
So within the language, it is irrelevant (an implementation detail) whether the variable is zero'd - as you cannot legally look to see. The CLR may behave differently, but since this is a C# NG...
Then, this is not a compiler error, contrary to what was suggested.
Thanks.
My apologies, it should read, "this is a compile* error" not compiler and
disregard the "I'm not sure if you can turn off this functionality", cause
obviously, you can't...
:)
HTH,
Mythran This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ChrisB |
last post by:
Hello All:
I notice that when using try/catch blocks in C#, variables declared in the
try block go out of scope in the finally block.
So, for example, the following code generates a compiler error:
try
{
SqlDataReader sqlDataReader = new SqlDataReader(sqlConnection,
|
by: clintonG |
last post by:
Has somebody written any guidelines regarding how to determine when
try-catch blocks should be used and where their use would or could be
considered superfluous?
<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
|
by: VB Programmer |
last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally.
Example: In order to close/dispose a db connection you have to dim the
connection outside of the Try Catch Finally block. But, I prefer to dim them
"on the fly" only if needed (save as much resources as possible). A little
further... I may wish to create a sqlcommand and datareader object ONLY if
certain conditions are met. But, if I want to clean these up in the...
|
by: STom |
last post by:
I heard someone mention to me that the use of try catch exception handling
is very expensive (in relative terms of slowing an app down) if it is used
frequently. Of course they could not explain why.
Is this true? If so, why?
Thanks.
STom
|
by: Bob |
last post by:
Hi,
The compiler gives Warning 96 Variable 'cmdSource' is used before it has
been assigned a value. A null reference exception could result at runtime.
Dim cmdSource as SQlClient.SQLDataReader
Try
Set up the database read and do it.
Catch ex as system.exception
exception stuff here
Finally
| |
by: What-A-Tool |
last post by:
In the following code, "catch" is underlined in blue, and the error "the
variable fx is declared but never used" is displayed when I mouse hover over
it.
Am I doing something wrong, or do I just ignore it?
private void btnCalc_Click(object sender, System.EventArgs e)
{
|
by: tommaso.gastaldi |
last post by:
It would be useful, sometimes, when debugging, to disable
all the try /catch one has in the program (clearly not commenting them
out).
Any info or hint on that?
-tom
|
by: reycri |
last post by:
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
|
by: Jake K |
last post by:
How do I access variables that are set within a try/catch block? The
following code produces a use of unassigned local variable error:
string varOne;
try
{
//access database here, select, datareader, etc...
varOne = dtr.ToString();
}
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |