Standing on the shoulders of giants. RSS 2.0
# Thursday, January 12, 2006

Well, maybe...

 

public class GenericComparer : IComparer {

public GenericComparer( CompareDelegate doCompare ){

if( doCompare == null ){

throw new ArgumentNullException("doCompare");

}

this.doCompare = doCompare;

}

public int Compare( object x, object y ){

if( x == null ){

if( y == null ){

return 0;

}

return -1;

}

if( y == null ){

return 1;

}

return doCompare(x,y);

}

private CompareDelegate doCompare;

}

public delegate int CompareDelegate( object x, object y );

Update:
My colleague Branimir did a little experimentation and found this implementation is similar in performance to a 'regular' methodcall when sorting around 10.000 items.

LastComparer.zip (30.65 KB)
Thursday, January 12, 2006 1:42:19 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Codesnippet | Development
# Wednesday, January 04, 2006

See also: A horrible way to calculate a squareroot

using System;
using System.Query;

namespace SquareRoot
{
class Program
{
static void Main(string[] args)
{
int input = 16;
decimal margin = 0.01M;

Func<decimal, decimal> abs = (decimal x) =>
x < 0 ? -x : x;

Func<decimal, bool> goodEnough = (decimal guess) =>
abs(guess * guess - input) < margin;

Func<decimal, decimal> newGuess = (decimal guess) =>
(guess + input / guess) / 2;

Console.WriteLine( "Guess {0}", Try(1, goodEnough, newGuess) );
Console.ReadLine();
}

static decimal Try(decimal guess, Func<decimal, bool> goodEnough, Func<decimal, decimal> newGuess)
{
if (!goodEnough(guess))
{
return Try(newGuess(guess), goodEnough, newGuess);
}

return guess;
}
}
}

Wednesday, January 04, 2006 2:32:56 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Development
# Monday, January 02, 2006

But fun to write:

using System;

class MyClass {
public static void Main() {

int input = 16;
decimal margin = 0.01M;

Func<decimal, decimal> abs = delegate(decimal x) {
return x < 0 ? -x : x;
};

Func<decimal, bool> goodEnough = delegate(decimal guess) {

return abs(guess * guess - input) < margin;
};

Func<decimal, decimal> newGuess = delegate(decimal guess) {
return (guess + input / guess) / 2;
};

Console.WriteLine("guess {0}", Try(1, goodEnough, newGuess) );
Console.ReadLine();
}

static decimal Try(decimal guess, Func<decimal, bool> goodEnough, Func<decimal, decimal> newGuess) {

if (!goodEnough(guess)) {
return Try(newGuess(guess), goodEnough, newGuess);
}

return guess;
}


delegate ReturnType Func<U, ReturnType>(U guess);
}

Inspired by Joel and Chris.

Monday, January 02, 2006 2:37:31 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Development
# Thursday, December 08, 2005

Code to create a random validationKey and decryption for use in the machinekey element of the web.config.

Note: valid values for the decryptionKey length are 8 (for DES) or 24 (for 3DES), the resulting key is twice as long and for validationkey 20 to 64.

using System;

using System.Text;

using System.Security.Cryptography;

namespace Crypto {

public class KeyCreator {

public static void Main(String[] args) {

String[] commandLineArgs = System.Environment.GetCommandLineArgs();

string decryptionKey = CreateKey(System.Convert.ToInt32(commandLineArgs[1]));

string validationKey = CreateKey(System.Convert.ToInt32(commandLineArgs[2]));

Console.WriteLine("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", validationKey, decryptionKey);

}

static String CreateKey(int numBytes) {

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

byte[] buff = new byte[numBytes];

rng.GetBytes(buff);

return BytesToHexString(buff);

}

static String BytesToHexString(byte[] bytes) {

StringBuilder hexString = new StringBuilder(64);

for (int counter = 0; counter < bytes.Length; counter++) {

hexString.Append(String.Format("{0:X2}", bytes[counter]));

}

return hexString.ToString();

}

}

}

source: mskb312906

Thursday, December 08, 2005 3:33:01 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Codesnippet
# Wednesday, December 07, 2005

The lowest level useful description of the goal of any software is probably best put as "elicit a positive emotional response
in a human".

2005 – DJ Mort

Wednesday, December 07, 2005 5:13:51 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Development

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Title>sourcesafe header</Title>

<Shortcut>header</Shortcut>

<Description>Code snippet for sourcesafe header.</Description>

<Author>Paul van Brenk</Author>

<SnippetTypes>

<SnippetType>Expansion</SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Declarations>

<Literal />

</Declarations>

<Code Language="csharp" Kind="file" Delimiter="*">

<![CDATA[#region SourceSafe header

// $Author: $

// $Modtime: $

// $Workfile: $

// $Revision: 1 $

#endregion

*end*]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>

File Attachment: sourcesafe_header.zip (550 bytes)

Wednesday, December 07, 2005 2:27:25 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Codesnippet | Visual Studio 2005
# Tuesday, December 06, 2005
Stef wrote an extension to NDoc, which can be used to create XmlDoc files for your database.
Tuesday, December 06, 2005 8:47:33 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Development
# Sunday, November 27, 2005

Update: Still haven't found what is causing the exception, but I think the message of the exception is incorrect. For now I'm sticking with the Knuth Shuffle algorithm from the PowerCollections. As suggested by Cyrus.

int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

System.Random rand = new System.Random((int)DateTime.Now.Ticks);

Array.Sort(ints, delegate(int x, int y){

// since you’re not allowed to return anything else than 0 for x.CompareTo(x)

if (x == y) {

return 0;

}

return rand.Next(-1,2);

}

);

note: This still throws an exception sometimes, claiming x.CompareTo(x) didn't return 0. I haven't found out why this happens or a pattern when this is happening, but I'll keep looking.

Sunday, November 27, 2005 1:10:40 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Codesnippet
# Saturday, November 19, 2005

Off course it was released the day after I built a new virtual machine for running the September CTP. Anyway, this release does support Visual Studio 2005 RTM, including the express editions.

Download here:

WinFX Runtime Components November CTP

SDK for WinFX Runtime Components November CTP

Visual Studio 2005 Extensions for WinFX

Visual Studio 2005 Extensions for Windows WorkFlow Foundation

Remember to remove all the old versions of WinFX, VS 2005 and .Net 2.0. Or start with a clean virtual machine.

The installation order is:

From Rob Relyea’s install guide for the September CTP.

 

Saturday, November 19, 2005 9:35:50 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Visual Studio 2005 | WinFX
About
© Copyright 2008
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.8275.16006
All Content © 2008, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)