I am writing a WPF application that searches, parses and writes files based on some minimal user input. I have a method call (to a method in another class) in my mainwindow class that requires a number of different pieces of information parsed from two separate files. Currently, I have another method that parses the source files and returns an array of the parsed data for another function of the application. Within that method, I have some public variables to store the necessary info for the first call. This is the only way I can figure how to access those items without writing separate methods to search the parsed data for each individual piece of info I have attached some code to help clarify my current situation:
//From mainwindow.xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
string[] path = ff.Search(fw.sysName, fw.design, fw.partNumber);//From FileFinder class
string[] parsedData = fp.Parse(path);//From FileProcessor class
string[] summary = fw.SummaryWriter(parsedData, fw.masterList);//From FileWriter class
Output.Text = "";
foreach (string str in summaryvalues)
{
Output.Text += (str + "\n");
}
}
The variables used in ff.Search() and fw.SummaryWriter() are called from FileWriter.cs:
class FileWriter
{
public string sysName;
public string design;
public string partNumber;
public List<string> masterList = new List<string>();
public string[] MasterWriter(string input, int source)
{
//Do stuff. The data for the public variables is mined from this method.
}
public string[] SummaryWriter(string[] parsedData, List<string> moduleMasterList)
{
//Do stuff
}
}
The question I have is simply, how do I access those variables without making them public?
First, rather than having public fields how about encapsulating them in public properties that expose private fields? Example:
See this site for info about properties
Second, when i run into methods that may need to return multiple values, you can either make out parameters to the method or encapsulate the return values into a class of their own and just return an instance of that class:
See this site for the out keyword
or
where SomeMethodOutput is a class with two properties.