Click here to Skip to main content
Click here to Skip to main content

Range Constructor

, 22 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) .

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.

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

/// 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 Smile | :) .

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

Share

About the Author

csinge
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralA thought PinmemberPaul Brower8-Sep-05 4:19 
Nice job.
 
Another feature you could add to this would be a 'Maximum Items' property or percent. This could allow even better use of the 'space' you have.
 
For example, if you had the maximum number set to 5, and you had the following items
 
Adam
Arnold
Barbara
Bill
Bert
Burt
Bob
Charley
Dick (sorry, had to use it)
Peter
Tom
Sarah
 
.. you might have buttons captioned as
 
1. Ad - Be
2. Bu - Pe
3. To - Sa
 
I realize there are variables not addressed here, but you get the idea of where this is going.
 

 

 
-- modified at 10:20 Thursday 8th September, 2005
GeneralRe: A thought Pinmembercsinge8-Sep-05 5:38 
GeneralNice Contribution PinprotectorMarc Clifton8-Sep-05 2:59 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Sep 2005
Article Copyright 2005 by csinge
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid