Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Build Stack Array for Formatting or Searching Data

Rate me:
Please Sign up or sign in to vote.
3.80/5 (37 votes)
3 Jul 20042 min read 58.5K   920   14   9
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

C#
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


Written By
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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:37
professionalManoj Kumar Choubey26-Feb-12 21:37 
GeneralGood Work Pin
mrGreenjeans28-Jul-04 8:29
mrGreenjeans28-Jul-04 8:29 
GeneralRe: Good Work Pin
Dave Brighton29-Jul-04 14:27
Dave Brighton29-Jul-04 14:27 
GeneralRelevant Pin
Member 125044621-Jul-04 7:03
Member 125044621-Jul-04 7:03 
GeneralRe: Relevant Pin
Dave Brighton21-Jul-04 7:30
Dave Brighton21-Jul-04 7:30 
GeneralEnlightening Pin
20-Jul-04 8:37
suss20-Jul-04 8:37 
This article is so inspiring. I was on the edge of my seat from beginning to end.

You remind me of someone I know. Are you related to someone famous? They are probably really good
looking too. Bold, masculine, personalities with such vivid imagination.;)






Can I get your autograph?
GeneralRe: Enlightening Pin
Dave Brighton20-Jul-04 16:45
Dave Brighton20-Jul-04 16:45 
GeneralSourceCode not available Pin
Antonio Barros5-Jul-04 1:39
professionalAntonio Barros5-Jul-04 1:39 
GeneralRe: SourceCode not available Pin
Dave Brighton5-Jul-04 9:59
Dave Brighton5-Jul-04 9:59 

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.