I have a function that is recursive in C# that i want to edit a global variable(i assume it is global due to the public before it) declared outside the function. For some reason that i do not know it cannot see the public variable within that specific function. It can see it in the first function in my code, but not in the second where i need to access it and change it to save a LOT of time opening a LOT of files…
Is there any reason why it wouldnt be accessable? and if so, how can i either get around it?
Thanks so much in advance!
public int[] timeInfo = new int[2];
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles())
{
int check =0;
try
{
string s = "";
s = directoryInfo.FullName + "\\" + file.Name;
List<string> row, row2, row3 = new List<string>();
using (StreamReader readFile = new StreamReader(s))
{
row = (readFile.ReadLine().Split(',').ToList());
try
{
row2 = (readFile.ReadLine().Split(',').ToList());
//timeInfo[0] = row2[0];
}
catch { check = 1; }
try
{
row3 = (readFile.ReadLine().Split(',').ToList());
//timeInfo[1] = row3[0];
}
catch { }
}
TreeNode[] headerNodes = new TreeNode[row.Count];
for (int i = 0; i < row.Count; i++)
{
headerNodes[i] = new TreeNode(row[i]);
if (check == 1)
{
headerNodes[i].BackColor = Color.Red;
headerNodes[i].ForeColor = Color.White;
}
}
directoryNode.Nodes.Add(new TreeNode(file.Name, headerNodes));
}
catch
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
}
return directoryNode;
}
The second function is static, and the variable exists only in the context of an object.