Tagged: code

Design Patterns : Singleton

A Singleton, in software engineering, is a design pattern that is used when you only want/need to allow one instance of a class. This is specifically useful when the class instantiation of the said class is resource expensive (e.g. database connection, graphics driver, network resource and file streams…).

Below is a UML describing the implementation details of a Singleton.


Here is an example of a Thread-safe Singleton implemented in C#. In this example, we are trying to simulate a DbConnection object. The DbConnection object has a private constructor that blocks users of this class from ‘new-ing’ this object. Instead, the DbConnection object has a property called Instance that allows users creates a copy of DbConnection.


Running the above code will have this output:



How To: Determine what Adobe Flash version is currently installed in a machine using C#

Here are two ways you can determine what version of Adobe Flash is currently installed in a machine using C#.

1. You can read the Registry entry for Adobe Flash player located in HKEY_LOCAL_MACHINE\SOFTWARE\MACROMEDIA\FLASHPLAYER and read the value for CurrentVersion key

2. Use Reflection by creating a new SWFObject (Flash Object) and reading its version property

Both functions will give you the version number in the form of a string for the currently installed Adobe Flash in your machine.

I personally preffer the Reflection method since it is cleaner and not dependent on registry key paths.

Expression Studio 4 is out and my 60-day tool

I was really excited last Monday after seeing Expression Studio 4 has been released (I have to admit that I’m one of those developers who logged in at MSDN at 12 midnight to check if the RTM bits are already out. hehe). I think this is great news for all XAMLers all over the world. On thing that I’m really sad about though is that our corporate MSDN license only has Expression Studio 4 Professional and not the Ultimate version. Bummed. So I ended up downloading the 60 day trial up until I can figure out how I can get a copy of the full version (OK, here’s the part that I ask donations to get a full copy but I’ll leave that out. LOL).

Sad, but still happy. At least I have 60 days to enjoy this new tool. 😉

How To: Count the occurrence of a character in a string in SQL

Last night I was trying to cleanup the spammers from the database of devpinoy.org and while I was evaluating the result sets i was able to conclude that aside from using common spam text like ‘cheap’, ‘buy’, ‘free’, ‘deal’, ‘viagra’, ‘prozac’ that 30% of the false emails that spam accounts are using multiple dots on their email address. A good example is a subset below from the list of offenders that I found in the devpinoy db.

Having found that fact I immediately created a sql script that will delete users from the database if they have more than 2 dots in their email address.

Enough with the side note and here is some code.

What the code above is doing is that it is removing the characters that matched our search key and then subracts the length of that string to the original string to find the total occurrence of the character we are looking for.

Now, if you want to use this as a function you can use this:

The code above works great but there’s a catch. If you are concerned with case sensitivity then the code above wont work. The way around it is to use COLLATION which is supported by the SQL function below:

In order to use this in your query all you need to do is

Or if you want to put it to use to meet the criteria that I mentioned about dots on emails you can do it this way: