Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/intimida/public_html/index.php:7) in /home3/intimida/public_html/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 431

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/intimida/public_html/index.php:7) in /home3/intimida/public_html/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 431
April « 2006 « Intimidating MONKEY

Archive for April, 2006

Find a DOM child element by name (Javascript)

Please, someone tell me there is a built in way to do this. But in the interim, since I don’t know of another way, if you’re looking for a child of a given node (by name), you can do it something like this.

    // Used to store the matched node, if found.
    var _jsMatchedNode;
 
    function jsFindChildByName(pvNodeName, pvParent)
    {
        if (pvParent.nodeName.toLowerCase() == pvNodeName.toLowerCase())
        {
            _jsMatchedNode = pvParent;
            return;
        }
 
        if (!pvParent.childNodes) return;
 
        if (pvParent.childNodes.length == 0) return;
        var nodeList = pvParent.childNodes;
        for(var x=0;x<nodeList.length;x++)
        {
               jsFindChildByName(pvNodeName, nodeList[x]);
        }
    }

Of course if you use this repeatedly it would behoove you to reset the _jsMatchedNode variable before each use.

Thou shalt not explicitly DataBind

Mmmmm. Remember that the Page.DataBind does this for you, explicitly calling control.DataBind is probably a bad idea.

See:
Do you use Page.DataBind?
Avoid calling Page.DataBind and bind each control individually to optimize your data binding. Calling Page.DataBind recursively calls DataBind on each control on the page.

You’re best bet is to override DataBind and call base.DataBind()

C# Array Initialization

An important reminder from our sponsors (oh, and just admit it… You can never remember this anyway):

From the 1.2 language spec:

The new operator automatically initializes the elements of an array to their default value, which, for example, is zero for all numeric types and null for all reference types.

Important notes about Array.Initialize()

It must not be used on reference-type arrays.

You can use this method only on value types that have constructors; however, value types that are native to C# do not have constructors.

And of course, if you don’t trust me, see for yourself.

List<> Interleaving

Ever wanted to interleave multiple sources of data? Well I did, so I wrote this, which interleaves multiple (generic) lists into a single.

public static List<telement> Interleave<telement>(params List<telement>[] pvParamLists)
{
    // if nothing is provided we'll simply return an empty list
    if (pvParamLists == null) return new List<telement>();
 
    int listCount = pvParamLists.Length;
 
    // offset map indicating current position in a given list
    int[] pointerMap = new int[listCount];
 
    // count total elements
    int resultSize = 0;
    foreach(List<telement> list in pvParamLists)
    {
        if (list != null)
        {
            resultSize += list.Count;
        }
    }
 
    // create our result list
    List<telement> interleavedList = new List<telement>(resultSize);
 
    // will quit when finished
    while (interleavedList.Count < resultSize)
    {
        for (int x = 0; x < listCount; x++)
        {
            if (pointerMap[x] != -1)
            {
                List<telement> currentList = pvParamLists[x];
                // skip null or empty lists
                if ((currentList == null) || (currentList.Count == 0) || (pointerMap[x] >= currentList.Count))
                {
                    pointerMap[x] = -1;
                    continue;
                }
 
                interleavedList.Add(currentList[pointerMap[x]++]);
            }
        }
    }
 
    return interleavedList;
}

The whole file, including some unit tests is here.