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

Range Constructor

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
22 Sep 20053 min read 32.1K   227   14   4
Range resizing...emulating Microsoft Outlook address book buttons.

Sample Image - RangeConstructor.png

Introduction

This is my first article. I've wanted to write something for ages...anyway I have read heaps of articles and haven't seen anything similar...so I hope u enjoy this one :).

The article covers a group of static functions that are useful for creating two dimensional arrays from one dimensional arrays. This is used in conjuction with custom button and panel classes to emulate the Microsoft Outlook address book buttons panel. It takes a range/array of objects and converts this array into a two dimensional array depending on the range size allocated (sorry for the long winded explanation, see the background to make more sense).

Background

I recently had an assignment which involved creating a teledex/electronic address book. I wanted to be able to have buttons on the left side that corresponded to the starting letter of a contact's name. One problem with this was that there needed to be room for 26 buttons. I wanted to be able to get the range displayed on the buttons to expand, when there was not enough room to fit all the buttons.

That is, if I had a range of say {A,B,C,D}, and if I had room for four buttons then it would be :

A
B
C
D

If I had room for only two buttons then it would appear:

A-B
C-D

For any one who uses Microsoft Outlook 2003, the address book uses this kind of resizing technique. So this library tries to emulate the Outlook address book feature.

Overview

For the assignment, the range class was using strings, but I thought I would change this to objects, as people might find other uses that I was unaware of, or didn't think of. Also another interesting fact is that the Microsoft uses a strange pattern when the range changes, this class uses a more standard form, but I provided an emulation to be able to change the type of distribution for future upgrades.

The Range Constructor

This is the important part of the static functions. This constructs a 2D array from a 1D array. The pattern that is used to form the distribution from 1D to 2D is dependent on the emulation range.

C#
/// Used to calculate a 2 dimensional array from a 
/// 1 dimension array, depending on the space allocated.
static public object[][] RangeConstruct(object[] range, int a, 
                   CameronSinge.Range.Range.Distribution dist)
{
    int required = range.Length;
    int allocated;
    object[][] tempRange = new object[a][];
    if(a == 0)
    {
        for(int i = 0; i < tempRange.Length; i++)
        {
            tempRange[i] = new object[0];
        }
        return tempRange;
    }
    if(required <= a)
    {
        allocated = required;
    }
    else
    {
        allocated = a;
    }
    int[] matrix = 
          CameronSinge.Range.Range.calculateStaggedMatrix(required, 
                                                        allocated);
    for(int i = 0; i < allocated; i++)
    {
        tempRange[i] = new object[matrix[i]];
    }
    int count = 0;
    for(int i = 0; i < allocated; i++)
    {
        for(int j = 0; j < tempRange[i].Length; j++)
        {
            tempRange[i][j] = range[count];
            count++;
        }
    }
    return tempRange;
}

The distribution

Okay...now all this time I kept talking about the distribution, but not really explained exactly what I meant. The distrubtion pattern is how the objects are distrubited over a two dimensional array.

The diagram below shows the standard form of distribution and gives some insight on how Microsoft Outlook 2003 distributes the objects.

Standard Distribution

C#
/// Creates a stagered matrix/array, 
/// used to create the standard type of distrubtion
static public int[] calculateStaggedMatrix(int required, int allocated)
{
    int [] staggedMatrix = new int[allocated];
    for(int i = 0; i < required; i++)
    {
        staggedMatrix[i%allocated]++;
    }
    return staggedMatrix;
}

Point of interests

I like the idea of converting 1D arrays into 2D arrays. I can see a lot of uses with it. One future use would be to have similar group of classes that are used to create tournament/team ordering.. (i.e., round-robin events, elimination, double elimination and so on...). If you want to discuss ideas please drop me a line :).

To-Do List

  • Make the library more OO - (any suggestions would be great!)
  • Add more forms of distribution
  • Change the class from using objects to make it generic
  • Include a hybrid version of the class that uses the most common letter distribution....

History

  • 23 Sep, 2005 - Included a diagram to explain the distribution easier.

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
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseNice article Pin
Rahul VB21-Oct-16 12:07
professionalRahul VB21-Oct-16 12:07 
GeneralA thought Pin
Paul Brower8-Sep-05 4:19
Paul Brower8-Sep-05 4:19 
GeneralRe: A thought Pin
csinge8-Sep-05 5:38
csinge8-Sep-05 5:38 
GeneralNice Contribution Pin
Marc Clifton8-Sep-05 2:59
mvaMarc Clifton8-Sep-05 2: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.