Friday, April 8, 2011

Interface ambiguity and implicit implementation

A while ago I had a small problem with interface ambiguity. The subject can be quite tricky to understand, so I decided to write a small blog post about the problem. This problem occurs when to interfaces implement the same property and a third interface tries to implement that property. Any references to that property through the third interface will result in a wonderful Ambiguity between 'xxx.yyy' and 'zzz.yyy'.

So let's jump in and consider the following interfaces:

public interface IShippable
 string Size { get; set; }
 string LabelText { get; set; }

public interface IAnimal
 string Size { get; set; }
 string Name { get; set; }

public interface IZooAnimal : IAnimal, IShippable
 string ZooName { get; set; }

Now let’s implement the interface by the following class:

public class Giraffe : IZooAnimal
 public string ZooName { get; set; }
 public string Size { get; set; }
 public string Name { get; set; }
 public string LabelText { get; set; }

The number of interface properties to implement by the Giraffe class is 5, but due to the implicit implementation, we only need to implement 4 properties. The compiler is able to figure out that the Size property is the implementation of both the IShippable and IAnimal interface.

Now let’s look at the problem. It might look like all is well, but consider the following program:

class Program
 static void Main(string[] args)
  Giraffe giraffe = new Giraffe();
  giraffe.Name = "Iwana";
  giraffe.ZooName = "Artis, Amsterdam, The Netherlands";
  giraffe.Size = "2m";
  giraffe.LabelText = "Tool cargo";


 public static void PrintSize(IZooAnimal animal)
  Console.WriteLine("Animal {0} is {1}", animal.Name, animal.Size);

It looks great, but this piece of code is not able to compile. The animal.Size will give the following error: Ambiguity between 'Tester.IAnimal.Size' and 'Tester.IShippable.Size'.

Saturday, January 22, 2011

Create Mp3 Playlist with PowerShell

I love mp3’s, especially now my phone has enough room to store a significant amount of mp3’s. I don’t know about you, but I’m a nut about categorizing my mp3 collection. Most of the time I’ll use the free version MediaMonkey to get the right meta data into the files (high res album covers) and I’ll use it to reformat the filename into something I like, like: Rihanna - Rated R - 01 - Mad House.mp3.

Playlist are also a dandy way of organizing the files into one list (easy to find on your device). The m3u playlist format is a no-hassle straight forward format, that’s why I love it. It is basically a list of relative file paths. Well… if it is that simple, it should be easy to write into a PowerShell statement. And it turns out, it was:

dir 'Rihanna - Rated R*.mp3' | 
    foreach { $_.Name } | 
        Out-File 'Rihanna - Rated R - 00.m3u'

Hope it helps.

Tuesday, December 21, 2010

Picasa MXF Analysis Script

Lately I’ve been working on a PowerShell script to fix the chronological order of files within a Picasa FaceMovie project file (MXF). It has been quite an ordeal, but I’m getting a lot of helpful feedback from the community (thanks you guys!).

We still have a problem with some files and that’s why I wrote up an analysis script to analyze all the files in the MXF file and create a report (.liz) from it. So, if you have problems with the script, please send me the .mxf, .mxf.backup and the .liz file and I’ll check if I can fix the problem.

Without further ado, here’s the script:

Sunday, November 28, 2010

Comparing Two Files in PowerShell

Sometimes you want to test if two files are the same. You could run MD5 or SHA hashes of the files, but it might take some time to compute them. A byte by byte comparison might be the fasted. After seeing some code on StackOverflow, I've decided to "port" the code to a PowerShell script. It might do some good for all the PS-ers out there.

Monday, November 22, 2010

Zooming an IFrame in IE

Today I was working with one of our designers to fix some iframe zooming in an Internet Explorer application. Unfortunately the zoom property is not supported by any other browsers than IE (I wish it was, and any suggestions are welcome!!).
Basically we wanted the iframeto be of a certain size and then zoom its page to that size. It sounds really easy… but...

This is the HTML we want to use:

<iframe id="tst" style="border:0; width:500px;height:200px;" src="http://sites.google.com/a/googleuniversity.org/ski-club/Home/About-the-Club" >
<p>Your browser does not support tampering with iframe content.</p>

That will result in:

In this post I will explain how to accomplish zooming the page as a minified preview of what it would look like if the page would be shown in a "normal" browser window. I will be using some jQuery because it solves a lot of nasty javascript problems for me and it makes the code easier to understand and read.

Saturday, November 20, 2010

Seek Position of a String in a File or FileStream

Yesterday I was working on a little bit of code to sniff out some XMP without having to worry about reading a file at a certain pre-defined position. XMP, being just plain XML, can be found by matching a string. After some googling I’ve found out that lots of people use code that read the entire file into memory and perform a regex or a string comparison. That’s not going to work for me, because I have files that are +100MB! So I wrote a class that reads the file byte for byte to search for that string. The method will return the position of the string in the specified file or FileStream.

I've wrapped the code in a simple class:

Wednesday, November 17, 2010

$1 Regex Replacement Problem PowerShell [solved]

Madre mia! I've been trying to edit an HTML file using PowerShell. I only wanted to eliminate all the spans and paragraphs using regular expressions (would make sense)... and how hard can that be?
Well, it turned out to be pretty hard if you're used to the replacement syntax of regular .Net expressions! After searching for hours I've discovered that $1 can't be used in the same way as in .Net. You will need to use ${1}. Ouch!



#get file
[string] $s = Get-Content "File.htm";

#replace spans
$regex = [regex] "<span.*?>(?<1>.*?)</span>"
$s = $regex.replace($s, '${1}')

#replace paragraphs
$regex = [regex] "<p.*?>(?<1>.*?)</p>"
$s = $regex.replace($s, '${1}')

#replace bold-end-tag
$s = $s.replace("</b>", "</b><br/>") 

#write result
$s | Out-File "Test.html" 

So, now you know!