michelc Blog

Just <strong>another</strong> WordPress.com weblog

Archive for the ‘c#’ Category

Retrouver un numéro de semaine

leave a comment »

public static int GetWeekNumber (DateTime dt) {
	CultureInfo culture = CultureInfo.CurrentCulture;
	int intWeek = culture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
	return intWeek;
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/856)

Written by michel

4 novembre 2005 at 4:39

Publié dans c#, Code Snippets

Convertir un type String en Enum

leave a comment »

enum EngineType {
	unknow, 
	access, 
	db2, 
	mysql, 
	odbc, 
	oledb, 
	oracle, 
	postgre, 
	sqlserver
}
string cnxTypeString = "mysql";
EngineType cnxTypeEngine = EngineType.unknow;
if (Enum.IsDefined(typeof(EngineType), cnxTypeString)) {
	cnxTypeEngine = (EngineType) Enum.Parse(typeof(EngineType), cnxTypeString, true);
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/824)

Written by michel

20 octobre 2005 at 1:15

Publié dans c#, Code Snippets

Algorithme pour calculer Pâques

leave a comment »

/// 
/// Algorithm for calculating the date of Easter Sunday
/// (Meeus/Jones/Butcher Gregorian algorithm)
/// http://en.wikipedia.org/wiki/Computus#Meeus.2FJones.2FButcher_Gregorian_algorithm
/// 
/// A valid Gregorian year
/// Easter Sunday
public static DateTime EasterDate(int year) {
    int Y = year;
    int a = Y % 19;
    int b = Y / 100;
    int c = Y % 100;
    int d = b / 4;
    int e = b % 4;
    int f = (b + 8) / 25;
    int g = (b - f + 1) / 3;
    int h = (19 * a + b - d - g + 15) % 30;
    int i = c / 4;
    int k = c % 4;
    int L = (32 + 2 * e + 2 * i - h - k) % 7;
    int m = (a + 11 * h + 22 * L) / 451;
    int month = (h + L - 7 * m + 114) / 31;
    int day = ((h + L - 7 * m + 114) % 31) + 1;
    DateTime dt = new DateTime(year, month, day);
    return dt;
}
  • Lundi de Pâques : Easter Monday = Easter Sunday + 1
  • Jeudi de l’Ascension : Ascension Day = Easter Sunday + 39
  • Dimanche de Pentecôte : Pentecost Sunday = Easter Sunday + 49
  • Lundi de Pentecôte : Pentecost Monday = Easter Sunday + 50

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/765)

Written by michel

27 septembre 2005 at 7:29

Publié dans c#, Code Snippets

Capitalization

leave a comment »

D’après C# Regular Expressions :

using System.Text.RegularExpressions;

public class MyClass {

	public static void Main() {
		string text = "the quick red fox jumped over the lazy brown DOG.";
		System.Console.WriteLine("text=[" + text + "]");
		string result = Regex.Replace(text, @"w+", new MatchEvaluator(MyClass.CapText));
		System.Console.WriteLine("result=[" + result + "]");
		System.Console.ReadLine();
	}

	static string CapText(Match m) {
		string temp = m.ToString();
		temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
		return temp;
	}

}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/705)

Edit : commentaire de utagger :

Here’s a shorter version:

protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = Regex.Replace(TextBox1.Text, @ »\b\w », new MatchEvaluator(stam));
}

protected string stam(Match m) {
return m.Value.ToUpper();
}

(the trick is using \b which is a 0-length match of word boundaries, including ^ and \s)

Written by michel

14 septembre 2005 at 4:58

Publié dans c#, Code Snippets, regex

yyyymmdd to DateTime

leave a comment »

DateTime myDate;

myDate = System.DateTime.ParseExact("20050802",

                                    "yyyyMMdd",

                                    System.Globalization.CultureInfo.InvariantCulture);

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/541)

Written by michel

2 août 2005 at 4:56

Publié dans c#, Code Snippets

DateTime to yyyymmdd

leave a comment »

string myString = myDate.ToString("yyyyMMdd");

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/540)

Written by michel

2 août 2005 at 4:53

Publié dans c#, Code Snippets

Set ValueToCompare at run time

leave a comment »

Lorsque un contrôle CompareValidator est utilisé pour contrôler une date et que la valeur de comparaison est définie côté code, il faut formatter cette date au format de date courte :

myCompareValidator.ValueToCompare = myDateTime.ToShortDateString();

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/524)

Written by michel

29 juillet 2005 at 5:33

Publié dans c#, Code Snippets

Générer une chaine aléatoire

leave a comment »

///
/// Build a random string (for id, login, password...)
///
public static string randomString() {
int length = new Random().Next(6, 10);
return randomString(length);
}

///
/// Build a random string (for id, login, password...)
///
public static string randomString(int length) {
string tempString = Guid.NewGuid().ToString().ToLower();
tempString = tempString.Replace("-", "");
while (tempString.Length

(Publié à l'origine sur http://www.bigbold.com/snippets/posts/show/518)

Written by michel

27 juillet 2005 at 5:20

Publié dans c#, Code Snippets

Encoder une chaine en SHA1 et hexa

leave a comment »

public static string SHA1_ComputeHexaHash (string text) {
// Gets the SHA1 hash for text
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns SHA1 hexa hash
return hexaHash;
}

(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/517)

Written by michel

27 juillet 2005 at 5:18

Publié dans c#, Code Snippets

Encoder une chaine en MD5 et hexa

leave a comment »

public static string MD5_ComputeHexaHash (string text) {
// Gets the MD5 hash for text
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = md5.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns MD5 hexa hash
return hexaHash;
}

(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/516)

Written by michel

27 juillet 2005 at 4:45

Publié dans c#, Code Snippets