473,387 Members | 1,766 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,387 software developers and data experts.

Use of unassigned local variable 'fout'

1
Some body please help me.
I finds this code really correct.
However I am a beginner
The following code yields

Use of unassigned local variable 'fout' error in my pgm please help

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. namespace ConsoleApplication6
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             FileStream fout;
  14.  
  15.             try
  16.             {
  17.  
  18.                 fout = new FileStream(@"C:\abc1.txt", FileMode.Open);
  19.             }
  20.             catch(Exception exc)
  21.             {
  22.                 Console.WriteLine("0.Error on access"+exc.Message);
  23.             }
  24.  
  25.             try
  26.             {
  27.                fout.WriteByte((byte)'F'); 
  28.             }
  29.  
  30.             catch(Exception exc)
  31.             {
  32.                 Console.WriteLine("1.Error on write "+ exc.Message);
  33.  
  34.             }
  35.             fout.Close();
  36.             Console.ReadLine();
  37.  
  38.         }
  39.     }
  40. }
Jul 1 '09 #1
3 3846
tlhintoq
3,525 Expert 2GB
Line 13 you never assign anything to fout, so it is still null.
LIne 18 you try to assign something to it, but that has the potential to fail leaving fout at null
Line 27 then tries to use that yet-to-be-assigned fout
Jul 1 '09 #2
IanWright
179 100+
A better way to do it, as WriteByte is dependent on you opening the FileStream correctly.
Expand|Select|Wrap|Line Numbers
  1. try
  2.             {
  3.                 fout = new FileStream(@"C:\abc1.txt", FileMode.Open);
  4.                 fout.WriteByte((byte)'F'); 
  5.             }
  6.             catch(Exception exc)
  7.             {
  8.                 Console.WriteLine("0.Error "+exc.Message);
  9.             }
  10.             finally
  11.             {
  12.                 if(fOut != null) {fOut.Close()};
  13.             {
  14.  
Jul 2 '09 #3
Curtis Rutland
3,256 Expert 2GB
Right, you don't have to set up a try block for each potential failure, you can use one try with multiple catches (each for a different type of Exception) if you want.
Jul 2 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection...
5
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class,...
12
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just...
3
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
29
by: Joseph Geretz | last post by:
Use of unassigned local variable 'fs' Please see where I've indicated where the compiler is flagging this error in the method below. fs is initialized in the first line in the try block, so why...
9
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
22
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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...

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.