Sunday, 19 May 2019

C# Dirty Delegate Hack

Just a quick one for today. For a uni C# assignment I’ve had to implement a multi-delegate that processes a list of data, with the delegate consisting of three disparate functions which I also had to implement, according to a unit test spec. Data has to flow between each step of the delegate to produce the correct output. Now the usual way to do this would be to use pass-by-reference to alter the original data in-place, however in this circumstance it was not possible as the tests required the signatures of the delegate methods to pass-by-value. After thinking for some time, I came to the following solution. Beware, it’s fairly horrible.

In the classes containing the delegate methods, I define a helper function;

public static List<List<string>> S(List<List<string>> oldData, List<List<string>>newData) {
     for (var i = 0; i < oldData.Count; i++) oldData[i] = newData[i];
     return oldData;
}

Then in the delegate methods, I wrap the results expression in this helper function, passing the original list as the first parameter

public List<List<string>> StripWhiteSpace(List<List<string>> data)
     => S(data, data.Select(row => row.Select(val => val.Trim()).ToList()).ToList());

This then results in the new data being returned from the method (satisfies the tests) as well as the original data being replaced (allows the data to flow to the next delegate method). How does it work? It comes down to the fact that the data I’m working with here is non-primitive; passing it between methods is implicitly by reference rather than by value. It did take me a while to reach this conclusion, my initial thinking was that I’m working with lists of strings, and strings are primitive and therefor pass-by-value. When I realised I was actually working with Lists, which are non-primitive objects, it occurred to me that I could write a helper method to modify each member of the original List in-place. Because the new data is being placed back into the original List object, the same object of which is referenced later on, the data carries itself forward into those next methods of the delegate.

Saturday, 11 May 2019

Open Live Writer, Blogger and Google Drive now working

IMG_4791

Photo: Open Live Writer running on Windows 10, taken at Docklands Melbourne. This image was published to this Blogger blog with Open Live Writer.

Beginning from the next release of Open Live Writer (0.6.3), Blogger users will now be able to successfully post images to their blogs again. After being affected by the issue myself in my own usage of the software, I took it upon myself to develop and deliver a fix. Today, Open Live Writer will, instead of uploading to Google Photos using the now non-functional Picasa API, upload to Google Drive, publicly link-share the photo, and then embed the direct URL to the image within your post. This all happens automatically from within Open Live Writer, there are no work-arounds or hacks at play.