Reputation: 554
I have the following code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string Elementsfile;
if (File.Exists(Application.StartupPath + "\\checkfiles.lst"))
{
string path = Application.StartupPath + "\\checkfiles.lst";
// Open the file to read from.
foreach (string readText in File.ReadLines(path))
{
var elements = readText.Split('|');
Elementsfile = elements[0];
string HashString = elements[1];
string ByteSize = elements[2];
Console.WriteLine(Elementsfile + HashString + ByteSize);
}
}
string filePath = Elementsfile;
}
I am trying to declare these variables:
string Elementsfile = elements[0];
string HashString = elements[1];
string ByteSize = elements[2];
...And use them elsewhere within the class.
When I try to use them, for example: string filePath = Elementfile;
, I get the error
Use of unassigned local variable 'ElementsFile'
Upvotes: 0
Views: 299
Reputation: 38367
Other questions are generally right, but they overlook one potential bug in your code. As you loop through the lines, and modify the variables, you are overwriting the previous value each time. Thus once the loop completes, you will only have the values from the very last line. You probably want to do something more like this to parse the file into a list:
public class HashEntryModel { //name it whatever represents a single line in your file
public string ElementsFile { get;set;}
public string Hash { get;set;}
public string ByteSize { get;set;} //sounds like an integer? Perhaps instead of string declare as int and parse with int.TryParse?
}
public class SomeClass()
{
public List<HashEntryModel> HashEntries {get;set;}
public SomeClass(){
HashEntries = new List<HashEntryModel>();
}
public void SomeMethod()
{
string path = Application.StartupPath + "\\checkfiles.lst";
// Open the file to read from.
foreach (string readText in File.ReadLines(path))
{
var elements = readText.Split('|');
HashEntryModel current = new HashEntryModel();
current.ElementsFile = elements[0];
current.Hash = elements[1];
current.ByteSize = elements[2];
HashEntries.Add(current);//add the entry we just parsed to the list
}
//now anywhere in this class we can loop through the entries:
foreach(HashEntryModel item in HashEntries)
{
Console.WriteLine(item.ElementsFile + item.Hash + item.ByteSize);
}
}
}
Upvotes: 0
Reputation: 28737
c# has block scope. That means that a variable is only visible within the block that they are defined.
So you should define them at the correct level:
public class MyClass
{
string myClassVar; // this one is usable throughout the class
void MyMethod()
{
string myMethodVar; // This one is visible inside this method
string path = Application.StartupPath + "\\checkfiles.lst";
// Open the file to read from.
foreach (string readText in File.ReadLines(path))
{
var elements = readText.Split('|');
string Elementsfile = elements[0]; // these ones are visible inside the for loop
string HashString = elements[1];
string ByteSize = elements[2];
Console.WriteLine(Elementsfile + HashString + ByteSize);
}
}
}
To answer your question, you should define your variables outside of the method (or at least the for loop). Note though that if you do the foreach, only the last value that the loop writes will be saved, since every iteration will overwrite the values.
Upvotes: 2
Reputation: 44439
You have to define them in a broader scope, more specifically on instance level.
class X {
string ElementsFile;
string HashString;
void method(){
ElementsFile = "file.txt"; // notice how you just assign the values
HashString = "something"; // instead of redefining them as string
}
}
Now every non-static method in your class can use those variables. If you would redefine them inside the method then this would be valid as well, but it would hide the variables that are defined on instance level.
In your example you're defining them inside a foreach
block. This is another level deeper than instance and method scope. If you just want to re-use those variables inside the same method but outside the foreach
then you can adapt the same logic for that scenario:
class X {
void method(){
string ElementsFile;
string HashString;
foreach(something) {
ElementsFile = "file.txt";
HashString = "something";
}
// Use the variables here
}
}
Upvotes: 5
Reputation: 6366
Well you should just make them fields of your class and not local variables. Or you should make them at least declared outside the scope of a foreach loop to get them accessible in other code blocks of the same method.
Upvotes: 0