Sometimes is is needed to copy some information from source code in a column-based style (parts of several lines starting from some position inside the line). It turned out that there is very easy way to achieve that. Just need to use Shift + Alt during selecting area with the mouse.
How To: Convert String to InputStream
Here is a method that does convertion:
public InputStream stringToInputStream(String text) throws UnsupportedEncodingException {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
}
Posted in How To, Programming
Derek Sivers on 1 to 10 Rating System
I’m checking old bookmarks left earlier to be categorized and found very good quote from Signal vs Noise. Thats what Derek Sivers says:
Think about the different areas of your life (career, relationships, spiritual, health, etc.) – and rate your satisfaction in each area from 1 to 10. Go through every area you rated a 5, 6, 7, or 8 – and replace it with a 1! Never settle for “it’s not so bad” – and instead face up to what you really want.
This is quite the same as my conclusion made couple of month ago. Just one change: I would keep 8 as possible rate meaning that the time for dramatical changes in this area has come.
Posted in Misc
Virtual Functions in Constructors Trap
In .NET calling virtual function in constructor of a class is a bit tricky. While one can expect that implementation from current class will be invoked, runtime actually always calls implementation of the function from the most derived class of all possible. So if virtual method in child class doesn’t call implementation from parent one can get trapped and initialization will be done incorrect.
For example, using BaseClass and DerivedClass provided below
class BaseClass {
public string DefaultName {
get {
return _defaultName;
}
}
protected string _defaultName;
public BaseClass() {
InitializeDefaults();
}
public virtual void InitializeDefaults() {
_defaultName = "BaseClass";
}
}
class DerivedClass : BaseClass {
public DerivedClass() {
}
public override void InitializeDefaults() {
}
}
when creating instances of the classes DefaultName will be different:
BaseClass baseClass = new BaseClass(); // baseClass.DefaultName: "BaseClass" DerivedClass derivedClass = new DerivedClass(); // derivedClass.DefaultName: null
Posted in Programming
How To: Get Correct Messages for Exceptions Fired from .Net Compact Framework Classes
Sometimes message from exception that occurred inside .Net CF classes doesn’t look handly at all:
An error message cannot be displayed because an optional resource assembly containing it cannot be found.
The thing is that error messages are actually in the resource assembly which is not installed on the device. So to fix the problem you just need to install this assembly:
- Navigate to CompactFramework folder in the SDK which is “%Program Files%\Microsoft.NET\SDK\CompactFramework”.
- Navigate to Diagnostics folder under desired .Net CF version (“v3.5\WindowsCE\Diagnostics” for 3.5 or “v2.0\WindowsCE\Diagnostics” for 2.0).
- Copy resource CAB file to device according to the language you want and install it.
Posted in How To, Programming
Launching Several Instances of Skype
Recently company I’m working for decided to change internal IM client from Lotus Sametime to Skype. Since I also have personal Skype account I have to run two instances of Skype.
One option was to use command line parameters to start second instance, but it doesn’t work good.
A better way was proposed by a colleague of mine (thanks Bertrand). There is a handy utility called Skype Launcher that does exactly what I want and in a great way.
Posted in Tools
Disk Partitioning Utility
There is a great free utility to perform disk partitioning called Easeus Partition Master Home Edition which can be downloaded from download.com.
Posted in Tools
Donald Knuth’s First Computer
This morning I encountered an interesting article about first computer used by famous Donald Knuth. Article also contains some interesting facts from young Knuth’s life.
How To: Unit Test a Singleton
Unit testing a Singleton is not a trivial task because each test needs untouched Singleton object to keep all the tests isolated. One way to achieve this is to recreate underlying singleton object using reflection during test setup or teardown as described by Jonathan de Halleux and Omer van Kloeten.
But I think that a better way is to extract implementation from singleton into a separated class. This way we’ll have a class that contains all the logic to be tested and another class used to wrap it into a singleton.
public class LoadBalancer {
internal LoadBalancer() {
}
// LoadBalancer class implementation goes here...
}
public sealed class LoadBalancerSingleton {
private static readonly LoadBalancer _instance = new LoadBalancer();
private LoadBalancerSingleton() {
}
public static LoadBalancer Instance {
get {
return _instance;
}
}
}
Using InternalsVisibleTo attribute in this assembly we can allow test project to perform needed unit testing. Although technically it is possible to create another instance of the LoadBalancer class inside this assembly comments on constructor and common sense should prevent such a situation.
So to my mind it’s a good alternative to reflection approach.
Posted in How To, Programming
Always Ware Your Seat Belt
Awesome ad to popularize safety in car:

