Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Build Stack Array for Formatting or Searching Data

0.00/5 (No votes)
3 Jul 2004 1  
This article builds stacks to filter or format data.

Sample screenshot

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
    {
        // Clear txtBox

        txtDisplay.Clear();

        // Create Header

        txtDisplay.Text = "First Name" + "  " + "Last Name" + "  " + 
                    "Olympic Sport" + "\r\n\r\n";

        // Call SearchArray Function

        intArrayIndex = SearchArray( arrayBuild, 
                    lstSports.SelectedItem.ToString() );

        // Split string into Single Dimensional Array for formatting

        // Again we will split the string at commas

        char[] trimChars = {'\u002c'};
        arrayDisplay = Regex.Split( strDisplay, @",\s*");

        // Iterate through stack and format string for txtDisplay

        for( int i = 0; i < arrayDisplay.Length - 1; i += 3 )
        {
            // Format String for Display in textBox

            strFormatDisplay = String.Format("{0, -12}{1, -12}" + 
                        "{2, -12}\r\n", arrayDisplay[ i ], 
                        arrayDisplay[ i + 1 ], 
                        arrayDisplay[ i + 2 ]);
            // Append string to txtDisplay.Text

            txtDisplay.AppendText( strFormatDisplay );
        }
    }
    finally
    {
        //Clear string variable

        strFormatDisplay = "";

        // Disable bttnLoad

        bttnLoad.Enabled = false;
    }
}

public int SearchArray( string [] array, string key )
{
    // Initialize string variable 

    strDisplay = "";

    // Iterate through the stack, and define the condition for the

    // filter 

    for( int n = 0; n < array.Length; n++ )
    {
        if( array[n] == key)
        {
            // Pass loop counter to global variable

            intPass = n;
            // Concatenate String

            strDisplay += arrayBuild[n - 2] + "," + 
                          arrayBuild[n - 1] + "," + 
                          arrayBuild[n] + ",";
        }                        
    }
    return intPass;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here