473,406 Members | 2,404 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,406 software developers and data experts.

Simple question - use of unassigned variable error

Hello

I am trying to get the last lines value in a text file and display it
on screen / read it into a variable.

When I run the following code in a console I get the
following error

"Use of unassigned local variable "LastLine"

What is the problem?

using System;

namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int counter = 0;
string line;
string LastLine;
System.IO.StreamReader file = new
System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{

Console.WriteLine (line);
LastLine = line;
counter++;
}

file.Close();

Console.WriteLine (LastLine);
Console.ReadLine();

}
}
}

Nov 17 '05 #1
2 3375
Hi kaiser,
the compiler is telling you that it is possible that your code is using a
variable that has never been given a default value, in your case it is the
LastLine variable. This is because you do not initialize the LastLine
variable, then you have a while loop which may never be entered i.e. you do
not give LastLine a value, finally you try to output LastLine to the screen.

The compiler is smart enough to know the while loop may never be entered.
in your code when you declare lastline initialize it as well i.e.

string lastLine = String.Empty;

The compiler will then be happy :-)
Hope that helps
Mark R Dawson
"kaiser" wrote:
Hello

I am trying to get the last lines value in a text file and display it
on screen / read it into a variable.

When I run the following code in a console I get the
following error

"Use of unassigned local variable "LastLine"

What is the problem?

using System;

namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int counter = 0;
string line;
string LastLine;
System.IO.StreamReader file = new
System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{

Console.WriteLine (line);
LastLine = line;
counter++;
}

file.Close();

Console.WriteLine (LastLine);
Console.ReadLine();

}
}
}

Nov 17 '05 #2
Thanks Mark! Worked perfectly!

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: andrewcw | last post by:
Why is it that I get these errors on this function 1.) The out parameter 'Avalues' must be assigned to before control leaves the current method 2.) Use of unassigned local variable 'Avalues' ...
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...
2
by: Doug | last post by:
I am trying to work with an object and create it like so: EXTRA.Sessions oSessions = new EXTRA.Sessions(); This doesn't work because I get this error: 'Cannot create an instance of the...
3
by: Antonio | last post by:
Can somebody tell me what's wrong with this code? When I try to debug, I get "Use of unassigned local variable 'ip2se'. string ip2se; if(e.Item.Cells.Text == e.Item.Cells.Text) ip2se =...
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;
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
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.