0 Comments

Few years back I released and wrote about the NSQLFormatter. NSQLFormatter is an open-source SQL Beautifier / Formatter library written with C#.

The library is now available as a portable class library.You can get either the source code through GitHub or the PCL-compatible DLL through NuGet.

Source code: https://github.com/mikoskinen/NSQLFormatter-dotnet

Nuget: https://www.nuget.org/packages/NSQLFormatter/

0 Comments

Creating strings inside loops in VB.NET can cause subtle and easy-to-miss bugs. Here’s a simple example:

    Sub Main()

        For i As Integer = 1 To 4 Step 1

            Dim text As String
            text = text & i.ToString

            Console.WriteLine(text)

        Next

        Console.ReadLine()

    End Sub

As we are creating the textvariable inside the for loop, one would presume that this would ouput:

1

2

3

4

Instead, when run, we get this:

image

Check the compiler warnings and you’ll see:

Variable 'text' is used before it has been assigned a value. A null reference exception could result at runtime.

The problem can be easily fixed by assigning an empty value to text when it is declared:

image

Still, this can cause interesting issues if you don’t play close attention to compiler warnings.

0 Comments
  •   Posted in: 
  • UWP

We’re working with a Windows 8.1 Store App where we need to know the physical screen size of the display. Here’s some simple code which seems to do the trick in most cases:

            var width = Window.Current.Bounds.Width * (int)DisplayProperties.ResolutionScale / 100;
            var height = Window.Current.Bounds.Height * (int)DisplayProperties.ResolutionScale / 100;

            var dpi = DisplayInformation.GetForCurrentView().RawDpiY;

            var screenDiagonal = Math.Sqrt(Math.Pow(width / dpi, 2) +
                        Math.Pow(height / dpi, 2));

            return screenDiagonal.ToString();

Please note that the DPI will return 0 if you have two displays in duplicate mode: http://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.display.displayinformation.rawdpix

0 Comments

Windows Phone caches web requests aggressively. We’ve all seen a case where the first request to the web server works as expected but then the following requests fail to return up-to-date data. Instead of just adding the timestamp to the request URL, you can fix this issue on the server side.

Background

Here’s a simple app next to Fiddler which demonstrates the problem. The app issues a web request with every button click:

        private async void DoRequest_OnClick(object sender, RoutedEventArgs e)
        {
            var client = new WebClient();
            await client.DownloadStringTaskAsync("http://adafy.com");

            requestCount += 1;

            this.RequestCount.Text = requestCount.ToString();
        }

First, a screenshots where user has clicked the button the first time:

image

And here’s what happens when she clicks the button the second time:

image

As you can see from the Fiddler, no request is sent to the server: Windows Phone returns the web request’s response from the cache.

The most often seen solution to this is to add a timestamp to the URL, this way bypassing the Windows Phone’s caching mechanisms. Example:

image

The requested URL changes every time, this way bypassing Windows Phone’s caching.

Solution

All this is because of the web request’s response headers.Make the server return the following Cache-Control header and your app will work exactly as you want:

Cache-Control: public, max-age=15, must-revalidate

Where max-age defines how long (in seconds) your Windows Phone app will cache the request.

By changing the headers you can configure how the app behaves. For example:

  1. You can force your app to make a fresh request every time (set max-age to 0).
  2. You can set exact caching (in seconds) for the requests.

Setting the headers on ASP.NET MVC

ASP.NET MVC offers multiple ways of configuring cache-control header. For example you can set the header on every action:

Response.Headers.Set("Cache-Control", "public, max-age=15, must-revalidate");

Or you can create a filter which is automatically executed after each request. The following StackOverflow questions show multiple solutions:

http://stackoverflow.com/questions/7087859/how-do-i-add-site-wide-no-cache-headers-to-an-mvc-3-app

http://stackoverflow.com/questions/9234044/asp-net-mvc-and-ie-caching-manipulating-response-headers-ineffective

Setting the headers on Azure Blob Storage

If you use Azure Blob Storage as your app’s backend, you can set caching for each blob using the blob.SetProperties-method:

            var blob = container.GetBlockBlobReference(file);
            blob.UploadText(content);

            blob.Properties.CacheControl = string.Format("public, max-age={0}, must-revalidate", (int)cacheAge.TotalSeconds);
            blob.Properties.ContentType = contentType;

            blob.SetProperties();

0 Comments

I blogged about VB.NET’s IIF-statements few years ago. As I mentioned back then, they are a handy way to shorten the code and to make it more readable but also a great way to introduce subtle bugs into our systems. Here’s the part 2 two that blog post, dealing with If statements and nullable variables.

Given the following VB.NET code:

        Dim amount = 100
        Dim newAmount As Integer? = If(amount = 100, Nothing, 50)

        Console.WriteLine("New value: {0}", newAmount)

With the first glance it’s quite obvious that the app will print “New value:  ” as we’re setting newAmount to nothing. But when run, here’s what we see:

image

Strange. NewAmount isn’t “nothing” as we thought, it’s actually 0. To correct the issue, we have to cast the “nothing” to nullable integer:

        Dim amount = 100
        Dim newAmount As Integer? = If(amount = 100, CType(Nothing, Integer?), 50)

        Console.WriteLine("New value: {0}", newAmount)

And this time we get the desired output:

image