I am building a Winforms application with C# and there are 3 files I will be reading in and then iterating over them to perform some function to verify them. I’m unable to iterate and can’t seem to figure it out as I am learning Winforms and C#.
// store each file path into a string
string nailAssignmentResult = textBox1.Text;
string nailFixtureResult = textBox2.Text;
string nailContactResult = textBox3.Text;
The declaration above stores the file path from 3 textboxes into string variables for me to iterate through.
I have tried a foreach loop and for loop but I can’t seem to figure out how to iterate through them. I would like to parse the information in each file into list separate by a space.
namespace FixtureReports
{
public partial class btnFileSelect : Form
{
// contructor
public btnFileSelect()
{
InitializeComponent();
}
// this will do the verification
private void btnVerify_Click(object sender, EventArgs e)
{
// clear textbox each time its clicked
results.Clear();
//store each file path into a string
string nailAssignmentResult = textBox1.Text;
string nailFixtureResult = textBox2.Text;
string nailContactResult = textBox3.Text;
// error for if all three paths are empty
TextBox errorMessage = results;
if (nailAssignmentResult == "" && nailFixtureResult == "" && nailContactResult == "")
errorMessage.Text = "ERROR: Cannot have empty path for all three reports!!";
// verifying each file path exists
if (File.Exists(nailAssignmentResult))
errorMessage.AppendText("Nail Assignment Report Found");
if (File.Exists(nailFixtureResult))
errorMessage.AppendText("\r\nNail Fixture Report Found");
if (File.Exists(nailContactResult))
errorMessage.AppendText("\r\nNail Contact List Report Found");
// grab each file and iterate through each of them
// parse the contents into a list separated by a space horizontally
// for each line of data, what needs to be compared
// take in receiver pin and plug into brians function to get pylon
// compare to existing pylon designation in report and see if it matches
// have a counter for how many were correct
// if incorrect, display pylon blocks and receiver pins
}
// prompt for file selection
private void nailFixtureBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
// prompt for file selection
private void nailAssignmentBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog.FileName;
}
}
// prompt for file selection
private void nailContactBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox3.Text = openFileDialog.FileName;
}
}
}
}
>Solution :
In order to "iterate" you need a collection. You can create a simple array from the three variables:
var files = new[] {nailAssignmentResult, nailFixtureResult, nailContactResult};
and iterate over that, but if you have the "processing" in a function where you can just pass in the filename (and whatever other information is neeed, then you don’t need to iterate:
var assignmentResults = ProcessFile(nailAssignmentResult);
var fixtureResults = ProcessFile(nailFixtureResult);
var contractResults = ProcessFile(nailContactResult);
These are only two options – there are many ways that this could be done, including creating an iterator function that uses yield return
to return each value, but I see no need for anything fancier that a simple array of the values.