Introduction
I work with records built from StringBuilder
class in an external file. This external file is read record by record, and loaded into an array. This is necessary for the Random Access File Program. If I want to filter records however, I build dynamic vertical arrays that load each field. These are stacks. I also include a counter so that I can read how many records are loaded into each stack. Sometimes, this is the only information I need if I am passing data to charts or graphs on another page.
The external file is read normally with a �for
statement� looping through each record. A conditional statement then tests the record to see if that record contains the information I am looking for. These conditional statements can be nested, or use a variety of string manipulation to split strings into usable data. If a record is one of the records I am looking for, then the stack is built using the properties �get{}; set{};
� statements. A counter is then incremented.
These stacks are more versatile than my original array. I can display these records in a textbox, or include them in a report in my presentation layer. Graphs and charts are useful in this presentation layer to graphically present data. This adds color to a report, and these charts and graphs can be printed using the PrtSc
function of your computer. This can be be used to create a .jpg, or .png file which can be embedded in a report.
I then convert these stacks back to horizontal arrays for formatting purposes. It is interesting to see how data is manipulated from horizontal array to vertical stack, and then back to horizontal array after the record filtering has been completed. Note that in the stacks, Array1[i]
correlates to Array2[i]
correlates to Array3[i]
and so on. These records are still intact, they are just stacked on top of each other in "First In First Out" (FIFO) style.
For demonstration purposes, I will create a 2 dimensional array to read data from rather than create an external file. Iterating through this sample data then will be a simple way to demonstrate our array manipulation.
The stack is then created. This is a Single Dimensional Array that is used for formatting, and defining the filter conditional statement. The records can be iterated through, until matching records are found. Copies of these records are made, and then those copies are passed to the presentation layer.
Code Listing
private void bttnSearch_Click(object sender, System.EventArgs e)
{
try
{
txtDisplay.Clear();
txtDisplay.Text = "First Name" + " " + "Last Name" + " " +
"Olympic Sport" + "\r\n\r\n";
intArrayIndex = SearchArray( arrayBuild,
lstSports.SelectedItem.ToString() );
char[] trimChars = {'\u002c'};
arrayDisplay = Regex.Split( strDisplay, @",\s*");
for( int i = 0; i < arrayDisplay.Length - 1; i += 3 )
{
strFormatDisplay = String.Format("{0, -12}{1, -12}" +
"{2, -12}\r\n", arrayDisplay[ i ],
arrayDisplay[ i + 1 ],
arrayDisplay[ i + 2 ]);
txtDisplay.AppendText( strFormatDisplay );
}
}
finally
{
strFormatDisplay = "";
bttnLoad.Enabled = false;
}
}
public int SearchArray( string [] array, string key )
{
strDisplay = "";
for( int n = 0; n < array.Length; n++ )
{
if( array[n] == key)
{
intPass = n;
strDisplay += arrayBuild[n - 2] + "," +
arrayBuild[n - 1] + "," +
arrayBuild[n] + ",";
}
}
return intPass;
}