.NET

Don’t lose you mind with references

January 14, 2010 .NET No comments

There could be a lot of stupid troubles with references once you work with set of solutions which of them contains lot of projects.

Here is one of them.

I worked with one simple testing tool (TT) which lives in solutionT.

Testing Tool uses projects B and C in the solution1, i.e. has references to B and C.
(Please note that we are using references by assemblies locations.)

Project B also has reference to some commont project named A.

We moved our common projects both with project A to some other solution2.
Of course we changed references of projects B.
Very nice.

I did a bit of change to the A and wrote test for the B.Tests project and it works well in solution1.
But when I started my TT I got exception I fixed back again. I was thinking how could it be…
I rebuilt all solutions carefully but the same occurred.

Do you know what the problem is?
The problem is that the latest project in the graph i.e. my Testing Tool of course has a reference to a project A and that reference is obsolete, since it has old location…

Bottom line:
When you have troubles with references ensure that latest assembly in your references graph (executable one) has all references which are up-to-date and they are rebuilt. (file timestamp should be enough).


No comments


Regex Quick Example

December 22, 2009 .NET, Regex No comments

This is just another quick example of Regex pattern.

Next pattern matches “at least one symbol which is letter or digit at the begging and then exactly one delimiter ‘|’ and then at least one symbol which is letter or digit in the end“:

var valuePattern = new Regex(@”^w+|w+$”);
bool isValid = valuePattern.IsMatch(someStringValue);

Valid is:

ab123|cde45

Not valid are:

|ad
df|
adf||adf

and so on…

Links I’ve used:

Verify your Regex:  
http://www.pagecolumn.com/tool/regtest.htm
Cheat Sheet:
http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/pdf/


No comments


Implement IDisposable Correctly

November 16, 2009 .NET No comments

Running FxCop on some old stuff code gives me next error: “Implement IDisposable Correctly”.

And provided me with link to MSDN: http://msdn2.microsoft.com/ms244737(VS.90).aspx

I took look at existing code and it looked like:

        #region IDisposable Members
        void IDisposable.Dispose()
        {
            ShutdownServices();
        }
        #endregion

And here are lot of incorrect implementation of the IDisposable.
At first I added GC.SuppressFinalize(this); like here:

        void IDisposable.Dispose()
        {
            ShutdownServices();
            GC.SuppressFinalize(this);
        }

Of course it was not enough – it is needed to call Dispose(true) because my class is not sealed.
You also need to have re-overridden Finalize.

So code which passed FxCop verification looks like:

        #region IDisposable Members
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~ServiceLoader()
        {
            Dispose(false);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                ShutdownServices();
            }
        }
        #endregion

Please go to MSDN page and read it, since to know how to implement IDisposable correctly is important thing.


No comments


Measure execution time

October 21, 2009 .NET No comments

Today I was asked to find the best way of measuring execution time of DoSomeWork() method.
MSDN gives the pretty fine answer for this, but google gives too much links with using DateTime.Now to measure execution time.
System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.

Stopwatch Class provides a set of methods and properties that you can use to accurately measure elapsed time.

See MSDN:

Example code:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Stopwatch stopWatch = new Stopwatch();
  9.         stopWatch.Start();
  10.         DoSomeWork();
  11.         stopWatch.Stop();
  12.         // Get the elapsed time as a TimeSpan value.
  13.         TimeSpan ts = stopWatch.Elapsed;
  14.         // Format and display the TimeSpan value.
  15.         string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”,
  16.             ts.Hours, ts.Minutes, ts.Seconds,
  17.             ts.Milliseconds / 10);
  18.         Console.WriteLine(elapsedTime, “RunTime”);
  19.     }
  20.     static void DoSomeWork()
  21.     {
  22.       // code does hard work here :)
  23.     }
  24. }


No comments