deep copying objects in C#

C# — Tags :, , , — admin @ 21:26

One of the fastest ways to deep copy an object is to serialize it to a binary stream and unserialize it to a new object, in that matter:

  1.        /// <summary>
  2.         /// Creates a deep copy of a serializable object
  3.         /// </summary>
  4.         /// <typeparam name="T">Any type</typeparam>
  5.         /// <param name="serializableObject">serializable object</param>
  6.         /// <returns>cloned object</returns>
  7.         public static T CloneOf<T>(T serializableObject)
  8.         {
  9.             object objCopy = null;
  10.             MemoryStream stream = new MemoryStream();
  11.             BinaryFormatter binFormatter = new BinaryFormatter();
  12.             binFormatter.Serialize(stream, serializableObject);
  13.             stream.Position = 0;
  14.             objCopy = (T) binFormatter.Deserialize(stream);
  15.             stream.Close();
  16.             return (T) objCopy;
  17.         }
  18.  

Playing with winforms textbox border color.

Non classé — Tags :, , , — admin @ 20:12

This is a small hack class that adds a set color for input area borders (textbox, richtextbox, combos, etc.) Here is the result:

Screenshot

Screenshot

It all holds up in a single file:

  1.  
  2.     /// <summary>
  3.     /// This class adds border to input elements similar to Chrome
  4.     /// </summary>
  5.     public class FormBorderizer
  6.     {
  7.         /** PRIVATE MEMBERS **/
  8.         private Label m_lblBorder;                      // label used for borders
  9.         private static Color BORDER_COLOR = Color.Gold; // Border color
  10.         private static int BORDER_WIDTH = 2;            // Border width
  11.         private static Color BACK_COLOR = Color.AliceBlue;       // Back color
  12.  
  13.         /** CONTRUCTORS **/
  14.         /// <summary>
  15.         /// Constructor, adds borders to input areas
  16.         /// </summary>
  17.         /// <param name="Control.ControlCollection">collection of controls to parse</param>
  18.         public FormBorderizer(Control.ControlCollection controlCollection)
  19.         {
  20.             InitializeBorder(controlCollection);
  21.             AddBorder(controlCollection);
  22.         }
  23.  
  24.         /** METHODS **/
  25.  
  26.         /// <summary>
  27.         /// Initializes the border control
  28.         /// </summary>
  29.         /// <param name="controlCollection">collection</param>
  30.         private void InitializeBorder(Control.ControlCollection controlCollection)
  31.         {
  32.             m_lblBorder = new Label();
  33.             m_lblBorder.BackColor = Color.Gold;
  34.             m_lblBorder.Visible = false;
  35.             controlCollection.Add(m_lblBorder);
  36.             m_lblBorder.SendToBack();
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Adds a colored border to a collection of controls
  41.         /// </summary>
  42.         /// <param name="controlCollection">target collection</param>
  43.         private void AddBorder(Control.ControlCollection controlCollection)
  44.         {
  45.             foreach (Control c in controlCollection)
  46.             {
  47.                 // generate event handlers for input areas
  48.                 if (IsInputControl(c))
  49.                 {
  50.                     c.Enter += new EventHandler(c_Enter);
  51.                     c.Leave += new EventHandler(c_Leave);
  52.                 }
  53.                 else if (c is Panel)
  54.                 {
  55.                     new FormBorderizer(c.Controls);
  56.                 }
  57.                 else if (c is TabControl)
  58.                 {
  59.                     TabControl tc = (TabControl)c;
  60.                     foreach (TabPage tp in tc.TabPages)
  61.                     {
  62.                         new FormBorderizer(tp.Controls);
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Checks whether this control should hold a border or not
  70.         /// </summary>
  71.         /// <param name="c">target control</param>
  72.         /// <returns>true or false</returns>
  73.         private bool IsInputControl(Control c)
  74.         {
  75.             return (c is TextBox || c is ListBox || c is ComboBox || c is RichTextBox || c is MaskedTextBox);
  76.         }
  77.  
  78.         /** EVENTS **/
  79.         /// <summary>
  80.         /// Event occuring when the user leaves an input control
  81.         /// </summary>
  82.         private void c_Leave(object sender, EventArgs e)
  83.         {
  84.             m_lblBorder.Visible = false;
  85.             Control ctrl = (Control)sender;
  86.             ctrl.BackColor = Color.White;
  87.            
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Event occuring when the user enters an input control
  92.         /// </summary>
  93.         private void c_Enter(object sender, EventArgs e)
  94.         {
  95.             Control ctrl = (Control)sender;
  96.             WrapControl(ctrl);
  97.             m_lblBorder.Visible = true;
  98.             m_lblBorder.Anchor = ctrl.Anchor;
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Wraps the border around the control
  103.         /// </summary>
  104.         /// <param name="control"></param>
  105.         private void WrapControl(Control control)
  106.         {
  107.             m_lblBorder.Top = control.Top - BORDER_WIDTH;
  108.             m_lblBorder.Left = control.Left - BORDER_WIDTH;
  109.             m_lblBorder.Width = control.Width + 2 * BORDER_WIDTH;
  110.             m_lblBorder.Height = control.Height + 2 * BORDER_WIDTH;
  111.             control.BackColor = BACK_COLOR;
  112.         }
  113.  
  114.     }
  115.  

To call it, simply create an instance of FormBorderizer (such an awful name!!) and initialize it with a form / panel control collection.
Ex:

  1.    public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.             new FormBorderizer(this.Controls);
  7.         }
  8.  
  9.     }
  10.  
  11.  

Winforms flicker-free forms.

C/C++, Non classé — Tags :, , — admin @ 0:12

Ever had those flickers in your 50+ component forms?

Trying to play with SuspendLayout / ResumeLayout didn’t help? That’s because it only suspends the automatic layout, triggered by the Anchor and Dock properties.

Setting double-buffering to true didn’t help either? That’s because it only suppresses flicker on individual controls: a label, a button and so on…

Setting the OptimizedDoubleBuffer flag to true? Nope, no changes…

After struggling for many hours trying to eliminate a flicker that was occuring on my application form, I finally found a solution on msdn:

It’s called compositing double-buffering and it’s simply 7 lines of code put in somewhere in your form code:

  1.  
  2. protected override CreateParams CreateParams {
  3.   get {
  4.     CreateParams cp = base.CreateParams;
  5.     cp.ExStyle |= 0×02000000;
  6.     return cp;
  7.   }
  8. }

Explanation from nobugz:

I discovered a new Windows style in the SDK header files, available for Windows XP and (presumably) Vista: WS_EX_COMPOSITED. With that style turned on for your form, Windows XP does double-buffering on the form and all its child controls

nobugz is the man…

Adding programs to path.

Non classé — Tags : — admin @ 15:11

By default, some programs will be installed while automatically being setup in order to be run from the command line.
For ex., [Win]+R ‘devenv’ launches Visual Studio.

I happened to run into this article which precises how to add some more:

Registry: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths

  1. Create a new sub-key with the name of the executable file that you wish to add to the path (ex. eclipse.exe)
  2. In this new key, add a string variable named “Path” containing the value of the the path to your new executable file (ex. “C:\Program files\eclipse\”)
  3. The new key will already have an empty variable (Default). Edit it to have the string value of entire address of the new program executable (ex. “C:\Program files\eclipse\eclipse.exe”)

Java foreach.

Java — Tags :, , — admin @ 17:36

Java 5 got a bit closer to its twin C# by adding the possibility of directly accessing entities of a list or collection through its own “foreach” functionality :

C# :

  1. foreach(type var in arr){
  2.   // implement body
  3. }

Java :

  1. for (type var : arr) {
  2.     // implement body
  3. }

List.Find() use of delegates

C/C++ — Tags : — admin @ 22:02

Delegates make it easier to find stuff in generic lists.
Let’s consider the following simple class:

  1. Public Class Person{
  2.   private string m_name;
  3.   private int m_age;
  4.  
  5.   // .Net3.0 syntax only.
  6.   public string Name{get;set}
  7.   public int Age{get;set}
  8.  
  9.   public Person(string name, int age){
  10.     m_name = name;
  11.     m_age = age;
  12.   }

Instanciating :

  1. List<Person> lstPpl = new List<Person>();
  2. lstPpl.Add(new Person("Christian", 1));
  3. lstPpl.Add(new Person("Noah", 0.3));

We can easily find Noah:

  1. Person myson = lstPpl.Find(delegate(Person p) { return "Noah" == p.Name; });

Encrypted binary serialization

C/C++ — Tags :, — admin @ 0:18

For re-use: a small class that does encryption and binary serialization:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Security.Cryptography;
  7. using System.Windows.Forms;
  8.  
  9. namespace MyNamespace
  10. {
  11.     class Tools
  12.     {
  13.         // change me…
  14.         private static string m_encryptionKey = "password";
  15.  
  16.         public static byte[] Encrypt(byte[] plainData, string sKey)
  17.         {
  18.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  19.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  20.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  21.             ICryptoTransform desencrypt = DES.CreateEncryptor();
  22.             byte[] encryptedData = desencrypt.TransformFinalBlock(plainData, 0, plainData.Length);
  23.             return encryptedData;
  24.         }
  25.  
  26.         public static byte[] Decrypt(byte[] encryptedData, string sKey)
  27.         {
  28.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  29.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  30.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  31.             ICryptoTransform desDecrypt = DES.CreateDecryptor();
  32.             byte[] decryptedData = desDecrypt.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  33.             return decryptedData;
  34.         }
  35.  
  36.         public static void SaveObjectToFile(object obj, string path){
  37.             try
  38.             {
  39.                 MemoryStream memStream = new MemoryStream();
  40.                 BinaryFormatter binFormatter = new BinaryFormatter();
  41.                 binFormatter.Serialize(memStream, obj);
  42.                 byte[] encryptedBytes = Encrypt(memStream.ToArray(), m_encryptionKey);
  43.                 memStream.Close();
  44.                 Stream streamToFile = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  45.                 streamToFile.Write(encryptedBytes, 0, encryptedBytes.Length);
  46.                 streamToFile.Flush();
  47.                 streamToFile.Close();
  48.             }
  49.             catch (Exception e)
  50.             {
  51.                 MessageBox.Show(e.Message);
  52.             }
  53.         }
  54.  
  55.         public static object LoadObjectFromFile(string path)
  56.         {
  57.             try
  58.             {
  59.  
  60.                 Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  61.                 byte[] encryptedObj = new byte[fileStream.Length];
  62.                 fileStream.Read(encryptedObj, 0, (int)encryptedObj.Length);
  63.                 MemoryStream memStream = new MemoryStream(Decrypt(encryptedObj, m_encryptionKey));
  64.                 BinaryFormatter binFormatter = new BinaryFormatter();
  65.                 object decryptedObj = binFormatter.Deserialize(memStream);
  66.                 memStream.Close();
  67.                 fileStream.Close();
  68.                 return decryptedObj;
  69.             }
  70.             catch (Exception e)
  71.             {
  72.                 MessageBox.Show(e.Message);
  73.             }
  74.             return null;
  75.         }
  76.     }
  77. }

Masters without style.

Non classé — Tags : — admin @ 18:05

Everytime I start digging into a new technology, I find myself quickly pointed towards some of the most talented computer science geek’s home page. And when I say talented, I mean “genius”… Those are the founding fathers of CS; those are the Ones who shift right four times the price tags of their supermarket nachos to know how much their 3 friends will owe them… In their head of course… Doing so, I eventually find myself hitting their own personal page, clicking that mouse link with as much enthusiasm as a little boy opening up his first nintendo…

And everytime, it happens… I find myself mixed with feelings of admiration and deception. Something like : “this guy is a genius… but he sure has an ugly personal webpage…”

Why is that??

Those guys are pioneers, they have proved to be inovative and creative… When one would expect to see some webpage using advanced techniques, stunning css, unique algorithms, or a perfect mix of simplicity, beauty and speed; we usually end up seeing repulsive design. Instead of seeing an echo of their genius, we see our screen filled up with 100% width / height text, using a max of 3 tags (”a”, “li” & “p”), and webpages that smell like old history books, the web’s first steps, my first 2400bps modem, …

Apart from the nostalgia it creates, I can understand how such a page can have some advantages : “who cares about heavy pages, buggy javascripts that slow down everything, bling-bling UIs that compensates for crappy contents? Let’s make a simple page, it’s fast, easy to maintain, it works everywhere the same, it’s stable, etc…” But still people, there is a balance in all things, even in web design… A little padding here, a little line-height there, some fonts, colors, divs, a bit more space and TADA! Your page becomes so much easier to read, much more attractive and friendlier. All of those little details that keep the speed and the functionalities while bringing a bit of warmth to a rich-yet-cold webpage.

Or has it become a stamp for quality? An ugly, old website now stands for quality content… ? It seems that’s the case for a couple of amazing books, including SICP… :

Or is it a question of humility? pride? I display myself as somewhat simple while having done extraordinary things?

Anyways… I’m not here to judge, I don’t have a personal web page! just this simple wordpress generated blog… I just found it somewhat funny. See it for yourself :

Les blocs conditionnels If, Case, etc…

C/C++ — Tags :, — admin @ 21:55

Il m’arrive souvent de voir des blocs de code de contrôle inutilement complexes, mais je suis tombé récemment sur un bloc qui m’a particulièrement bluffé :

  1. if( iNb < 100 ) {
  2.   if( iNb > 50 ) {
  3.      [bloc 1]
  4.   } else if( iNb > 30 ) {
  5.      [bloc 2]
  6.   } else if( iNb > 10 ) {
  7.      if( iNb <= 20 ){
  8.         [bloc 3]
  9.      } else {
  10.         [bloc 4]
  11.      }
  12.   }
  13. }

Lorsque l’on remplit les [...] avec des dizaines de lignes de code (ce que je déconseille aussi fortement), on se retrouve vite à oublier le contrôle mal organisé qui se planque dans le code…

Il existe une pratique simple, qui doit être mise sous forme de théorie quelque part sur le net, qui consiste d’abord à visualiser les ensembles sur lesquels on fait le contrôle. Ici, on peut facilement déterminer 4 ensembles que l’on essaye de distinguer:
iNb dans ]50, 100[ (bloc 1)
iNb dans ]30, 50 [ (bloc 2)
iNb dans ]20, 30 [ (bloc 3)
iNb dans ]10, 20 [ (bloc 4)

Ensuite, le principe de base est le suivant : on conditionne sur le plus grand conteneur d’abord, et on restreint par la suite. On se retrouve alors avec un code beaucoup plus clair :

  1. if( iNb < 100 ){
  2.   [bloc 1]
  3. } else if( iNb < 50 ) {
  4.   [bloc 2]
  5. } else if( iNb < 30 ) {
  6.   [bloc 3]
  7. } else if( (iNb < 20) && (iNb > 10) ){
  8.   [bloc 4]
  9. }

Autre possibilité, encore plus lisible je trouve :

  1. if((iNb > 50) && (iNb < 100)){
  2.   [bloc 1]
  3. } else if ((iNb > 30) && (iNb < 50)){
  4.   [bloc 2]
  5. } else if ((iNb > 20) && (iNb < 30)){
  6.   [bloc 3]
  7. } else if ((iNb > 10) && (iNb < 20)){
  8.   [bloc 4]
  9. }

On redécrit les ensembles directement au sein des conditionnelles, de manière à ce que l’on ait pas à retrouver la condition précédente pour bien comprendre l’actuelle… Enfin tout du moins c’est ce que je pense…

Bien implémenter son singleton

Java — Tags :, , , — admin @ 11:54

Lorsque plusieurs membres d’un programme vont utiliser les mêmes données, on se retrouve confronté au choix entre le singleton et la classe statique. Ce choix est assez important, et dépend entièrement de la visibilité de ces données. Il sera parfois plus judicieux d’utiliser une classe statique qu’un singleton. Par exemple, en programmant un jeu de tennis en C# (que je n’ai pas encore complètement terminé d’ailleurs…), j’avais besoin d’accéder souvent au calendrier ATP. La visibilité de ce calendrier était pratiquement globale, beaucoup de classes devaient y avoir accès et il était plus facile d’en faire une classe statique avec ses méthodes statiques… Dans d’autres cas, le singleton était plus approprié. Il n’est pas limité en accès aux membres statiques, il est sérializable, etc…

Récemment, en révisant du code sur un projet au boulot, je suis tombé sur des mauvaises implémentations de singletons de ce type :

  1. public class SinglePart {
  2.      private static SinglePart oPart = null;
  3.  
  4.      public static SinglePart getSinglePart() {
  5.          if (oPart == null){
  6.              oPart = new SinglePart();
  7.          }
  8.          return oPart;
  9.      }
  10.  }

La classe fonctionnait bien, puisque le programmeur avait fait attention à ne l’instancier qu’une seule fois. En fait, il y a 2 erreurs avec cette implémentation :

- Il manque un constructeur privé pour remplacer le constructeur public et donc éviter une autre instanciation via ce dernier.
- Rien n’assure que le singleton ne soit créé deux fois en même temps par deux threads séparés.

La solution consiste donc à ne pas oublier le constructeur privé, ainsi que l’utilisation du mot clef synchronized :

  1. public class SinglePart {
  2.      private static SinglePart oPart = null;
  3.  
  4.      // constructeur privé
  5.      private SinglePart() {}
  6.  
  7.      public synchronized static SinglePart getSinglePart() {
  8.          if (oPart == null){
  9.              oPart = new SinglePart();
  10.          }
  11.          return oPart;
  12.      }
  13.  }
Page suivante »
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2014 Random namespace | powered by WordPress with Barecity