Recording Video Demo of Application
What I Wanted
I was searching for good video capturing software to create a demo video for the game I’ll release soon. Looking for some simple functionality to capture on-screen video and place it to YouTube I thought it will take no more then an hour.
The Reality
However I ended with about three hours spent on this for several reasons:
- First I had a problem recording audio from the game
- Then many tools I tried were creating strangely wrong avi files
Happy End
I finally have it working with BB FlashBack Express like a charm with their function to export to YouTube
One bonus about it is how nicely it captures WP7 emulator window (with and without frame). No need to position capturing region. Perfect region is one click away with BB FlashBack Express highlighting the window.
Resolving audio recording issue
It turned out that I need to enable so called “Stereo Mix” in sound settings:
- Select “Sound” from the control panel.
- Select the “Recording” tab.
- Right click on the background of the tab and choose “Show disabled devices”.
- Right click on “Stereo Mix” and click “Enable”.
- Right click on “Stereo Mix” and click “Set as Default Device”.
I was surprised when didn’t found “Stereo Mix” it at all. Anyway after some web search and trials I solved this issue by updating sound card driver.
Dealing With Strangely Wrong Avi Files
I couldn’t resolve issue completely, but I found a great workaround. I’m uploading video to YouTube directly from BB FlashBack Express.
Implicit and Explicit Implementation of Interface
Difference Between Explicit and Implicit Implementation
Usually we implement interfaces implicitly. This has an advantage when we need to call method defined in interface: it can be called from variable either of type interface or class. In the same time explicitly implemented method cannot be called from variable of type class.
This is demonstrated by the code below:
namespace ExplicitInterfaceImplementation
{
class Program
{
static void Main(string[] args)
{
Sample sampleObject = new Sample();
CallExplicitlyImplementedMethod(sampleObject);
CallImlicitlyImplementedMethod(sampleObject);
Console.ReadLine();
}
static void CallExplicitlyImplementedMethod(Sample sampleObject)
{
// When uncommented next line will not compile.
//sampleObject.ExplicitMethod();
// This is how we can call explicitely implemented method.
ISample sampleInterface = sampleObject as ISample;
sampleInterface.ExplicitMethod();
}
static void CallImlicitlyImplementedMethod(Sample sampleObject)
{
// Next line will compile and execute without problems.
sampleObject.ImplicitMethod();
// Also we can call implicitely implemented method using interface.
ISample sampleInterface = sampleObject as ISample;
sampleInterface.ImplicitMethod();
}
}
interface ISample
{
void ExplicitMethod();
void ImplicitMethod();
}
class Sample : ISample
{
void ISample.ExplicitMethod()
{
Console.WriteLine("ExplicitMethod()");
}
public void ImplicitMethod()
{
Console.WriteLine("ImplicitMethod()");
}
}
}
One more important thing is that explicitly implemented method always public. For this reason C# doesn’t allow to use access modifier for the method.
Why Could We Need Explicit Implementation?
So explicit implementation narrows usage of the method. When can we have advantage of it in this case?
It is useful when implementing two interfaces containing methods with the same signature as shown in sample below:
namespace ExplicitInterfaceImplementationUsage
{
class Program
{
static void Main(string[] args)
{
Operations operations = new Operations();
IAddition addition = operations as IAddition;
ISubtraction subtraction = operations as ISubtraction;
Console.WriteLine(addition.Compute(1, 1));
Console.WriteLine(subtraction.Compute(1, 1));
Console.ReadLine();
}
}
interface IAddition
{
int Compute(int left, int right);
}
interface ISubtraction
{
int Compute(int left, int right);
}
class Operations : IAddition, ISubtraction
{
int IAddition.Compute(int left, int right)
{
return left + right;
}
int ISubtraction.Compute(int left, int right)
{
return left - right;
}
}
}
Actually we could implement Compute() method implicitly from one interface and explicitly from another one. Nevertheless I wouldn’t recommend such solution because it will be even harder for client code to decide how to call Compute() method correctly.
I must confess I never had to use explicit interface implementation. So samples here are fake ones.
Did you have to use it in production code? Was it something else then you couldn’t change method name in interfaces because its in third-party library?
How To: Read String Line by Line
Breaking a string containing multi-line text to a list of lines can be easily done using StringReader. Here is a method that does this and small test program:
class Program
{
static void Main(string[] args)
{
IList<string> lines = BreakLines("This is a test for breaking multiline string \nto a list of lines.");
foreach (var item in lines)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
private static IList<string> BreakLines(string text)
{
IList<string> lines = new List<string>();
using (StringReader reader = new StringReader(text))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
return lines;
}
}
“throw;” vs “throw ex;”
Sometimes exception gets caught to make some logging and rethrow it upper (i.e. log exception in library and then throw it to client code).
In this case it’s important to understand that “throw ex;” will cut stack trace. So when caught in calling code exception will contain StackTrace and TargetSite starting from the method that rethrown it. Sometimes it’s desirable to hide real stack trace, sometimes – not.
Lets take a look at following code:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.ClientMethod();
Console.ReadLine();
}
void ClientMethod()
{
try
{
LibraryAPI();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
void LibraryAPI()
{
try
{
LibraryInternal();
}
catch (Exception ex)
{
throw ex;
}
}
void LibraryInternal()
{
throw new Exception("Trace me if you can");
}
}
Although exception was thrown in LibraryInternal() method console output will be following:
at ConsoleApplication6.Program.LibraryAPI() in D:\Users\andrii.khymenko\Development\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35
at ConsoleApplication6.Program.ClientMethod() in D:\Users\andrii.khymenko\Development\ConsoleApplication6\ConsoleApplication6\Program.cs:line 19
By changing “throw ex;” to “throw;” in LibraryAPI() stack trace in console output will change to:
at ConsoleApplication6.Program.LibraryInternal() in D:\Users\andrii.khymenko\Development\ConsoleApplication6\ConsoleApplication6\Program.cs:line 41
at ConsoleApplication6.Program.LibraryAPI() in D:\Users\andrii.khymenko\Development\ConsoleApplication6\ConsoleApplication6\Program.cs:line 35
at ConsoleApplication6.Program.ClientMethod() in D:\Users\andrii.khymenko\Development\ConsoleApplication6\ConsoleApplication6\Program.cs:line 19
This difference in behavior has simple explanation: MSIL has two instructions “throw” and “rethrow” while C# has only one keyword “throw”. When code gets compiled all “throw;” lines end up with “rethrow” in MSIL which will keep original stack trace.
Assertion Classes in MSTest Framework
For the last couple of months I’ve been doing a lot of unit tests using MSTest framework. Although it is good to have special assertion classes to simplify verification of strings and collections names picked my framework developers are not very good as for me.
These classes are named StringAssert and CollectionAssert. Looks good at first glance. But took me some time to figure out they exist.
Lets think about context in which they are used. I’m doing assertions in tests and I definitely know about core assertion class named Assert. Suppose additional classes were named AssertString and AssertCollection. Small change in names would make dramatical change in discoverability. As I start typing for Assert class in IntelliSense I would see them next to core assertion class.
This example demonstrates that developers should think about usage context of classes when naming them.
We Love Windows Phone from Windows Phone Design Day
Metthew Bennett used a great way to explain his passion to WP7 in his presentation on Windows Phone Design Day (Making Audio Sing):
Fixing TortoiseCVS Icon Overlay in Windows 7
After installing TortoiseCVS on Windows 7 icons of folders and files that are under CVS control didn’t change. Which clearly dispappointed me. Some Web browsing and several tries lead me to the proper solution.
To fix the issue you need to rename subkeys under “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ShellIconOverlayIdentifiers” starting with “Groove” to something that will move them to the bottom of subkeys list (i.e. VGroove).
How To: Perform column-based selection in Visual Studio
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"));
}


