Why do I need to initialize the string variable 'name'? - c#

This was frustrating to solve. I kept getting the error "Use of unassigned local variable" on the 'name' variable in the line of code near the end where it outputs to the List Box.
The solution was to initialize the variable 'name'.
At first, I had simply declared the 'name' variable without initializing it. This threw the error of "Use of unassigned local variable". When I finally also initialized that variable (as shown below), the error went away.
But I don't understand this, because, by the time the code reaches that output line, the name variable does get assigned.
Can someone please explain WHY initializing the 'name' variable got rid of the compiling error?
'''
try
{
StreamReader inputFile; // StreamReader object to read the file
string line; // To hold a line from the file.
int row = 0; // Student/line counter.
int column = 0; // Counter for columns in CSV file.
int total = 0; // Accumulator for grades.
double average = 0; // Average test score
string name = ""; // each student's name
// Create delimiter array
// Why is this necessary? Why not just put the delimiter in the Split method call?
//char[] delim = { ',' };
// Open the CSV file
inputFile = File.OpenText("Grades.csv");
while (!inputFile.EndOfStream)
{
// increment the row counter
row++;
// Read a line from the file
line = inputFile.ReadLine();
// Get the test scores as tokens
string[] scores = line.Split(',');
// Set the accumulator to zero
total = 0;
// Calculate the total of the test score tokens
for (column = 0; column < scores.Length; column++)
{
if (column == 0)
{
name = scores[column];
}
else
{
total += int.Parse(scores[column]);
}
}
// Calculate the average test score
average = (double)total / (scores.Length - 1);
// Display the average
//averagesListBox.Items.Add("The average for student " + row + " is " + average.ToString("n1"));
averagesListBox.Items.Add("The average for " + name + " is " + average.ToString("n1"));
}
// Close the file
inputFile.Close();
// Display the total records read
totalLabel.Text = row.ToString("n0");
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
'''

The compiler cannot prove that it does get set anywhere. What if scores.Length is zero? Then there is no assignment. So the compiler is correct in throwing an error of possible situation where it is not assigned a value.
If you add a condition for scores.Length always being over zero the compiler might be able to deduce that a value is always set. And you could always set it directly to the first element in an array and loop starting from 1 anyway, which is much more efficient.

Actually, it it possible that you're using the name variable without initialization if column < scores.Length.
That's what the warning is about.

Related

Reading a txt file and sending it to an Array C#

I have a text file that looks like this
Words Words
Words Words
Words Words
1 34.4e+1
2 34.3e+1
3 34.2e+1
4 34.1e+1.... // and so on
I need to get the string number and concert it to decimal/double and then send it to an array where I can the use the array outside of the for loop to get the average via Enumerable.Chunk
decimal[] raw = new decimal[] { };
decimal[] rawAvgList = new decimal[] { };
decimal RawAvg = 0m;
try
{
string bPath = aPath + "\\" + fileName.Name + "\\textfilename.txt";
string[] readText = File.ReadAllLines(bPath);
readText = readText.Skip(3).ToArray();
foreach (var line in readText)
{
raw = new decimal[] { Decimal.Parse(line.Substring(9).ToString(), style1) };
for (int i = 0; i < raw.Length; i++)
{
Console.WriteLine("{0} \t {1}", raw[i], i++);
}
}
rawAvgList = raw.Chunk(20).Select(chunk => chunk.Average()).ToArray();
RawAvg = rawAvgList.Average();
}
So for when I call the array outside of the loop it only grabs the last number in the text file. Am I calling the information wrong? I swear I have tried all the different way to call the numbers from the text file and I just keep running into errors. The error range from it not liking me using skip and substring at the same time or and enumerable error where it returned the error and not the number. Anything to help, Thanks!
You are assigning the variable raw to a new value on each loop iteration, wiping out any value that was stored previously. The end result is that after the loop terminates, it will only contain the value from the last line in the file as you are seeing.
You can declare raw as a List<decimal> instead, then within the loop, you would do
raw.Add(Decimal.Parse(line.Substring(9).ToString(), style1));
This way, once the loop finishes, you'll have all the numbers and not just the last one.

How would I concatenate this line with a string?

Caliber = Convert.ToInt32(values[(int)TankData.Cannon]); //add measurement here
Okay so in my datagridveiw I want to add measurements to columns that need them but every time I try to concatenate this line with a string Caliber = Convert.ToInt32(values[(int)TankData.Cannon]) + " mm"; I keep getting the error of "Cannont implicitly convert type 'string' to 'int'
Your "Caliber" variable is Integer I believe. Of course, you cannot set a string value to an integer variable.
If you want to add measurement to a column, this could work
for (int i = 0; i < DataGridView.Rows.Count; i++)
{
DataGridView.Rows[i]["Caliber"] += " mm";
}

c# go to specific line in text file then skip a few lines and edit

I know how to read through all lines of a file and replace a selected line when a certain sequence of characters is found. The issue that I'm having at the moment is that I'm stuck with a structure that has no unique string to search for except for the main class name. So for example I'd know that the name of the class is "List_of_boats" and the structure tells me that 11 lines underneath that line is the value "items=2;" which I need to change to a certain value, depending on the amount of items I want to insert there.
Is there a way to use the foreach function or something to do this? I have provided some code that I've already got so far but I'm kind of stuck now.
var lines = File.ReadAllLines(fileToMerge);
var linID = 0;
foreach (var line in lines) {
if (line.Contains("ace_arsenal_saved_loadouts")) {
var newlinID = linID + 11; //go from ace_arsenal_saved_loadouts to "items=x;" to change number of items.
}
linID = linID + 1;
}
Convert the enumerable to an array, and loop through it by index:
var lines = File.ReadAllLines(fileToMerge).ToArray();
for (var linID = 0; linID < lines.Length; linID++) {
var line = lines[linID];
if (line.Contains("ace_arsenal_saved_loadouts")) {
var newlinID = linID + 11; //go from ace_arsenal_saved_loadouts to "items=x;" to change number of items.
}
}

IndexOutOfRange Exception Thrown on Setting Int value to Array

On my app a teacher can have several classes, and when I exclude a teacher's profile I have to, first, delete his classes. I'm trying to put each class_id of this teacher on an int array, to later delete all classes which the id is contained inside this array.
This is my code so far:
int x = 0;
int[] count = new int[x];
while (reader_SelectedClasses.Read())
{
if(x != 0)
{
x++;
count = new int[x];
}
count[x] = _class.Class_id = reader_SelectedClasses.GetInt16("class_id");
}
And this is what
reader_SelectedClasses.Read()
does:
select class_id from tbl_class where user_id = " + id + ";
And this is the return, when I try this on MySQL:
But it gives me back an IndexOutOfRangeException when I run the code on my DAO class. What am I missing? Already went here but didn't quite understand. Can someone please explain on few words and post and fixed code for this?
You need to learn how to use a debugger and step through your program.
count = new int[x]; discards what was in count and creates a new array that contains zeroes. This array's indexes go from 0 to x - 1.
count[x] = ... sets the array element at index x which according to the previous line is one past the end of the array.
You need to set count = new int[x] only once, at the beginning of your program, and set count[x] = ... only if x >= 0 and x < count.Length.
You are getting IndexOutOfRange Exception because you are trying to access element from array which is out of range.
At first line you are setting x = 1. Hoping that controller enters while loop and as x is 1 it doesn't enter if loop and it executes next statement. But count[1] (x = 1) is not allowed as you have created array with only one element and that you can access with count[0]. (Array indexing starts from 0)
You are trying to achieve a List behavior with an array.
Obviously, the IndexOutOfRangeException is because you initialize an empty array and then try to add values to it in a non-existing cell.
Try to convert to List<int>:
List<int> count = new List<int>();
while (reader_SelectedClasses.Read())
{
int classId = _class.Class_id = reader_SelectedClasses.GetInt16("class_id");
count.Add(classId);
}
If you really need an array out of it you can do:
int[] countArray = count.ToArray()

How to stop While Loop in c#

I have an array that are having some different values. one array called saveforRR this should work like whenever i have any value that less that 0, it will be removed from the array. Also, the values in the array must reduced by 1. I do not know how many iterations are going to take to make my array Length =0. so I used while loop. However, when i run the code my loop stops in this line fortheaddingvalue = (int)saveforRR[w]; //It says her that my array is out from boundaries, which i do not know how? How can I fix this problem? can you advise me please?
//Create variables and set up the arrays
int value = 7;
int [] saveforRR = {1,2,3,4,5,15,86};
//start get the values reduced
int fortheaddingvalue;
int w = 0;
//Run the while loop to reduce the value in the saveforRR array.
while(saveforRRName.Length !=0 && saveforRR.Length !=0)
{
fortheaddingvalue = (int)saveforRR[w]; //It says her that my array is out from boundaries, which i do not know how?
fortheaddingvalue = fortheaddingvalue - 1;
if (fortheaddingvalue > 0)
{
saveforRR[w] = fortheaddingvalue;
txtOutput.Text += "\r\n" +saveforRR[w] + " this is the value";
}
else
{
Array.Clear(saveforRR, w, 1);
txtOutput.Text += "\r\n" + saveforRR[w] + "this is the value Remover";
}
w++;
}
The length of your array will never be zero. The Clear method doesn't remove items from the array.
An array can't be resized. You can use the Resize method to get the same effect, as that will create a new array with a different size, and copy the items to it. That's however not efficient, you should use a List<int> instead, as that supports actual resizing.
It seems that nobody mentioned the break-statement. You can use it break out of any loop:
while(true)
{
// Eternal loop here...
// Break out
break;
}
Read more about it here: break
Other ways to exit loop include for example goto, return or setting the loop expression not to match

Categories

Resources