Posts

Showing posts with the label C#

Its the little things...

In the history of C# we have been spoiled by every version having excellent new language features in. We had Generics, Linq, Tasks, async/await which were massive hitters and transformed the way we worked and wrote code. C#7 however I found has given us lots of little improvements that make things smoother and easier which has been great. One feature I hadn't really used yet though was the new Pattern Matching , this wasn't because I hadn't wanted to but more I hadn't seen a time to really use it. That was until tonight. A year or so ago I had a scenario where I had to take a base type and then1 return the appropiate mobile view to render. Prior to Pattern Matching I ended up with something like: if (value is HoldingPage) return new LoadingView(); } var listingPage = value as ListingPage; if (listingPage != null) { return new ListingView(listingPage); } Tonight I needed to add a new view into this code and decided it needed tidying up. Pattern Matching...

Fixing a Quirky "Xamarin.Forms ListView Bug" That Led Me Back to Basics And How Most Bugs Are Written By Users!

Image
For the last two evenings I've been chasing what seemed a random bug. The crux of it came down to scrolling large lists within a ListView, 1000's of items, which were grouped, would muddle up the items and often duplicate an item a couple of times. An example output: First page of results: Test 1 Test 2 Test 3 Test 4 Test 5 Test 6 Test 7 Test 8 Test 9 Test 10 Test 11 Test 12 The second page would then render: Test 13 Test 14 Test 1 Test 15 Test 16 Test 4 Test 17 Test 18 Test 19 Test 20 Test 6 At first glance this looked like a random error. I considered that maybe my list parsing code (the app takes HTML and makes in easier to use) but my unit tests checked out. So I went over these again with a toothcomb. All checked out. Next I considered the code that grouped the items, again unit tests all good, toothcomb said still good. So I sat pondering. Was there something similar about all of the duplicated items. There was, they were all items that...

MVC DateTime Suffix HTMLHelper

Recently I have been working on an MVC Project, and tonight I got to the point where I wanted to output a date in a specific format, for example Tuesday 13th April 2010. Sadly DateTime formatting still doesn't allow you to specify output a suffix, you can do Tuesday 13 April 2010 but not what I wanted. I decided that I could achieve what I wanted by writing a quick HTMLHelper, this would then allow me to specify a datetime format string with a magic / special character which I could then replace for the appropriate suffix. Writing the helper was very quick and easy, if you want to learn about HTMLHelpers and how to write your own I recommend looking at Stephen Walther's Post on HTMLHelpers . The code for my datetimehelper is below: using System; using System.Web.Mvc; namespace mjjames.MVCHelpers { public static class DateTimeExtensions { public static string DateTimeFormat(this HtmlHelper helper, string dateTimeFormat, DateTime dateTime){ var dateTimeOutput = ...

Using DOTRas - An Overview and some things I've learnt

Yesterday I decided to starting knocking together a quick application to help me backup my server to some local storage. The idea being that at any point I have a local copy of my server setup a maximum of a day old. The point of this application and how I've gone about writing it, what libraries I'm using etc will be part of a future blog post. I decided early on that I however I wanted to transfer files I wanted to do this over a VPN to the server. I had several reasons for this, being able to expose my files over a network share, more secure etc. My Application will be running on an old laptop, so I first thought about just always having it connected to a VPN using windows, and run the application as normal. However I then thought what if the VPN disconnects and I don't notice, how long would it take until I noticed etc. So I decided to make the application create a VPN Connection at start up and then disconnect from it upon completion. I figured that there would be a go...

Book Review: C# 2008 and 2005 Threaded Programming

So last month Packt Publishing contacted me regarding sending me a promotional copy of C# 2008 and 2005 Threaded Programming to review. This is the first time I have been asked to do a book review and decided to take them up on the offer. Now I have been using ASP.Net for around three years now but I've never had to or decided to look into writing multi-threaded apps so the fact that this book was aimed at beginners meant that I was an ideal target audience for this book. Packt shortly sent me the book and upon first looking at it thought it looked a bit ugly! I know you can't tell a book by its cover but this cover did put me off, the green and picture didn't do it for me but alas I carried on anyway. The book is organised into several chapters and is example driven. What I mean by this is that it doesn't give you bags of theory and then an example, it takes the approach of you following along the code examples and then it has gaps explaining bits and pieces. More o...

Parsing an XML Boolean

Today I came across a situation where I was taking a value from an XML file which is a boolean. Now being me I knew that someone would either use 1 or true to indicate this boolean value, I for example always use 1 and 0 but I know people that prefer the "proper" way of saying true or false, especially if you don't have an XSD handy. The XML itself is fine however once I had loaded this XML file into my .Net application I needed to parse the value as a boolean. This is where I hit a roadblock. Bool.Parse will only parse "true" or "false" string values not "1" or "0". A quick explore through various sources led me to find XmlConvert .ToBoolean(), which is part of System.XML. XmlConvert allows you to convert from XML Data Types to .Net Data Types and in the case of Boolean can convert 1 to true. This saved me loads of time so I thought I'd post it up here for others to find and hopefully enjoy.