Click here to Skip to main content
13,833,733 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 3 Jul 2004

Build Stack Array for Formatting or Searching Data

, 3 Jul 2004
Rate this:
Please Sign up or sign in to vote.
This article builds stacks to filter or format data.

Sample screenshot


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

        // 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 );
        //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;


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


About the Author

Dave Brighton
Web Developer
United States United States
I studied Fortran IV in HighSchool where we had 2 keypunch machines, and access to an IBM 1100 at the Community College. We ran our programs batch, and compiled our programs on paper tape.

Years later when PC's became affordable, I gave programming another shot. This time I studied RPG with the IBM AS-400 computer. I could access the College Computer with Emulator Software( Mocha Soft MW 5250 ) and my home PC.

C++ came later, then VB-6, C#.Net, and Managed C++. I am currently studying VB.Net

You may also be interested in...


Comments and Discussions

GeneralGood Work Pin
mrGreenjeans28-Jul-04 9:29
membermrGreenjeans28-Jul-04 9:29 
GeneralRe: Good Work Pin
Dave Brighton29-Jul-04 15:27
memberDave Brighton29-Jul-04 15:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190114.1 | Last Updated 4 Jul 2004
Article Copyright 2004 by Dave Brighton
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid