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
C# « Intimidating MONKEY

Archive for category C#

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.

Copy via XmlSerializer

For God’s sake, don’t ever write this (at least not again). Its such a trivial piece of code that I never bother librarifying it, and yet every time I write it I leave out the reset of the stream position. But, what do you expect… I’M A MONKEY!!!

public static Object XmlSerializationCopy(Object pvValue)
{
  if (pvValue == null) return null;
  XmlSerializer ser = new   XmlSerializer(pvValue.GetType());
  // most are probably small.
  MemoryStream serStream = new MemoryStream(512);
  using(serStream)
  {
    ser.Serialize(serStream, pvValue);
    serStream.Position = 0;
    return ser.Deserialize(serStream);
  }
}