Archived entries for Server Side

Fisher–Yates Shuffle of SPListCollection in C#

I like working with c# when not with PHP as it helps me learn deeper ways to do things. Also it constantly makes me grateful for PHP where the following is as easy as shuffle($numbers); in PHP.

C#

public static int[] shuffle(int[] array)
{
Random rng = new Random();  // System.Random
// n is the number of items remaining to be shuffled.
for (int n = array.Length; n > 1; n--)
{
// Pick a random element to swap with the nth element.
int k = rng.Next(n);  // 0 <= k <= n-1 (0-based array)
// Swap array elements.
int tmp = array[k];
array[k] = array[n - 1];
array[n - 1] = tmp;
}
return array;
}

Disable Chrome in C# Sharepoint 2007 Webparts

I create a lot of custom webparts for SharePoint 2007. One slightly annoying thing is remembering to turn of the Chrome when adding webparts to pages. It can also be a problem when your advanced clients who are adding webparts forget to to disable the Chrome.

In your c# webpart class slap in the following code to automatically disable Chrome. You can’t even select anything other than Chrome so be careful.

public override PartChromeType ChromeType
{
get {
return base.ChromeType;
}
set {
base.ChromeType = PartChromeType.None;
}
}

PHP Range

Ever need an array of the alphabet or an array of sequential numbers.

The range() function in PHP does just that.

<?php
foreach(range('a', 'z') as $letter) {
echo $letter;
}
?>

This function will also work with numbers. This will print all the numbers from 1 to 12.

<?php
foreach(range(0, 12) as $number) {
echo $number;
}
?>

Also you can add a step parameter to the third argument.

<?php
foreach(range(0, 50, 10) as $number) {
echo $number;
}
?>

0, 10, 20, 30, 40, 50

I was wondering of there was an smarter way to print the whole alphabet in PHP than just creating an array containing all off the letters by my self.

I present to you, the range() function.

<?php
foreach(range('a', 'z') as $letter) {
    echo $letter;
}
?>

This function will also work with numbers. This will print all the numbers from 1 to 12.

<?php
foreach(range(0, 12) as $number) {
    echo $number;
}
?>

And if you are smart, and run PHP 5, you can even print every tenth number (0,10,20,30…)

<?php
foreach(range(0, 100, 10) as $number) {
    echo $number;
}
?>


Copyright © 2004–2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.