Vadim's Weblog

Never stop learning.

Archive for the ‘Uncategorized’ Category

Pex: Automated Explorary Testing for .NET is out.

Posted by Vadim on May 23, 2008

Jonathan ‘Peli’ de Halleux, original creator of MbUnit, and Nikolai Tillmann released Pex under Microsoft Research license.

In order to get better idea what Pex is, I would recommend to watch a screencast, it’s only 6 minutes long.

I also would recommend to listen to Scott Hanseman‘s podcast Hanselminutes where Scott interviews Peli and Nickolai.

Posted in Uncategorized | Leave a Comment »

Enum Conversion.

Posted by Vadim on October 19, 2007

In this post I will demonstrate how to convert Enum To String, String To Enum, and Value to Enum.

First of all let us create an enumeration:

   1: enum Colors
   2: {
   3:     Blue = 1, 
   4:     Green = 2, 
   5:     Red = 4
   6: }


Convert Enum To String.

It seams that nothing can be easier.  We easily can use ToString() method to convert Enum to String.  It would work fine if we try to convert Colors.Green, Colors.Blue, or Color.Red.  However, what if we try to convert to String value like this:

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: Console.WriteLine(myColor.ToString());

The output will be ‘3’ instead of ‘Blue, Green’.

There are two ways we can solve this problem:

We can just add FlagsAttribute to our enumeration.

   1: [FlagsAttribute]
   2: enum Colors
   3: {
   4:     Blue = 1, 
   5:     Green = 2, 
   6:     Red = 4
   7: }

Or another solution we can use Format() or ToString() methods with format “F” or “f”.

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "F"));
   3: Console.WriteLine(myColor.ToString("f"));

In case you decorated your enumertion with FlagsAttribute but want to output the value, use Format() or ToString() methods with format “D” for decimal form or “X” for hexadecimal one.

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: // Format()
   3: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "d"));
   4: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "x"));
   5: // ToString()
   6: Console.WriteLine(myColor.ToString("D"));
   7: Console.WriteLine(myColor.ToString("X"));

The method with format “D” will output ‘3’ and with format “X” will give us ‘00000003’.


Convert String To Enum.

To convert String to Enum we just need to use Parse() method.

   1: public Colors String2Enum(string colorString)
   2: {
   3:     return (Colors)Enum.Parse(typeof(Colors), colorString);
   4: }

In the example above colorString must match case exactly.  There’s an override for Parse() method where you can ask to ignore case.

   1: public Colors String2Enum(string colorString)
   2: {
   3:     return (Colors)Enum.Parse(typeof(Colors), colorString, true);
   4: }


Convert Value To Enum.

We will use ToObject() method to convert value to Enum.

   1: public Colors Value2Enum(int colorValue)
   2: {
   3:     return (Colors)Enum.ToObject(typeof(Colors), colorValue);
   4: }

Update: Thanks to Omer Mor (see his comment below) who pointed out that we don’t have to invoke ToObject() method.  We can simply cast value to Enum type.

   1: public Colors Value2Enum(int colorValue)
   2: {
   3:     return (Colors) colorValue;
   4: }

kick it on

Posted in Uncategorized | 4 Comments »

Watch out for escape characters.

Posted by Vadim on September 22, 2007

You have to be aware of the escape characters when you parse a file  character by character.  For instance if you try to read a file that contains “C:\bigFile.txt” you’ll get back “C:igFile.txt” or something like this.

If you don’t believe me, try it yourselves.

   1: public void ParseString()
   2: {
   3:     string text = "C:\bigFile.txt";
   4:     string newText = string.Empty;
   5:     foreach (char c in text)
   6:     {
   7:         newText += c;
   8:     }
   9:     Console.WriteLine(newText);
  10: }

Below I provided a table of escape character for you and myself to reference later.

Char ASCII Dec# ASCII Hex# ASCII Name Ctrl Char Description
00 00 NUL ^@ null
\a 07 07 BEL ^G Alert
\b 08 08 BS ^H Backspace
\f 12 0C FF ^L Form feed
\n 10 0A LF ^J New line
\r 13 0D CR ^M Carriage return
\t 09 09 HT ^I Horizontal tab
\v 11 0B VT ^K Vertical tab
\” 34 22 N/A N/A Double quote
\’ 39 27 N/A N/A Single quote
\\ 92 5C NA N/A Backslash
\u#### N/A N/A N/A N/A Unicode escape
\x## N/A N/A N/A N/A Unicode escape
\Uxxxxxx N/A N/A N/A N/A Unicode escape

Let me know if I miss anything.

Posted in Uncategorized | Leave a Comment »

Learn TDD with MbUnit

Posted by Vadim on July 12, 2007

Wednesday, July 18, I’m going to talk about TDD with MbUnit at Florida .NET user group.  If you are in Ft. Lauderdale area, you are  welcome to come.  Free pizza will be provided.  The meeting will take place at Microsoft Southeast District building at 6:30 PM.

Posted in Uncategorized | Leave a Comment »

Looking for good developers.

Posted by Vadim on June 27, 2007

The company I work for, Ultimate Software, is always in search for good, passionate C# developers to engineer its next-generation, Enterprise HR and Payroll software solution. The company is located in sunny South Florida where weather is always great. During my career as a developer I worked in many different companies but Ultimate Software is by far the best company I’ve worked for so far. The company earns top ranking on “Great Place to Work” third year in a row. This year, and last year we made #3 Best Medium-Sized Company to Work for in America.

Ultimate Software’s development environment is very Agile.  We practice Scrum methodology. We also doing TDD and our unit test framework is MbUnit.

If you want to work on the cutting edge of software development processes and technology, in an extremely programmer friendly environment with very smart, extremely knowledgeable developers, apply for one of Ultimate Software’s developer openings below.

Two developer positions currently open:


Posted in Uncategorized | 3 Comments »

Timer class

Posted by Vadim on April 8, 2007

I was checking the difference in performance between SqlDataReader and DataTableReader. I needed something to measure time with. I wrote a simple class Timer. It worked just fine for me; however, I find it hard to believe that .Net framework doesn’t have anything to measure time. The reason for this post is that I hope that someone can suggest a better solution to my Timer class.
Here’s there Timer class:

public class Timer
  long _start;
  long _stop;

  public void Start()
    _start = GetTimeInMilliseconds();

  public void Stop()
    _stop = GetTimeInMilliseconds();

  public long TimeElapsed
    get { return _stop - _start; }

  private long GetTimeInMilliseconds()
    return DateTime.Now.Hour * 60 * 60 * 1000
      + DateTime.Now.Minute * 60 * 1000
      + DateTime.Now.Second * 1000
      + DateTime.Now.Millisecond;

In case you’re interested in performance difference between SqlDataReader and DataTableReader. Here’re my findings:
On single CPU machine SqlDataReader was faster and it of course was expected. However, on double CPU machine DataTableReader was slightly faster and this was a pleasant surprise for me.

Posted in Uncategorized | 2 Comments »