My Instagram feed

My Instagram feed

2017-04-19

Get time for given country

This code gets the first time zone for a country (meaning it probably won't work if your country has more than one), and calculates the date/time given the UTC date/time.

The method is an extension to the DateTime type, meaning you can use it by typing .AsLocalTimeForGivenCountryFromUtc(countryCode) after a variable or function returning that type. The countryCode parameter is a string, and must be a two-letter ISO 3166-1 country code.

You need to import John Skeets NodaTime library from NuGet.

Code:
using NodaTime.TimeZones;
using System;
using System.Linq;

namespace MyNameSpace
{

    public static class DateTimeExtension
    {

        public static DateTime AsLocalTimeForGivenCountryFromUtc(this DateTime utcDateTime, string countryCode)
        {
            //no matter what tz info is on the date, assume it is UTC
            var dte = new DateTime(utcDateTime.Year, utcDateTime.Month, utcDateTime.Day, utcDateTime.Hour, utcDateTime.Minute, utcDateTime.Second, DateTimeKind.Utc);
            var tzMapping = TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones.FirstOrDefault(t => t.Territory == countryCode); //NodaTime
            var tz = TimeZoneInfo.FindSystemTimeZoneById(tzMapping.WindowsId);
            return TimeZoneInfo.ConvertTimeFromUtc(dte, tz);
        }
    }
}

2017-04-06

Visual Studio 2015 Freezes when ReSharper is running

Ok, so finally got ReSharper installed. Again. Been doing fine without it for a while, but the team is using it so I got myself a license. But it was a bit of a disappointment at first. It seems that whenever I build my solution, which has both some C# and one F# project, VS freezes. Today it even crashed after being unresponsive for quite a few minutes. Seems more people have the same problem:

https://resharper-support.jetbrains.com/hc/en-us/articles/206546149-Visual-Studio-with-ReSharper-is-freezing-and-or-crashing?flash_digest=fcf1e14381f3c8171cbc9129d0fa536af24a7a10

So what happened was that I was in my F# project making some changes, then I built the solution, and then I went on to do some more changes in the same project, but things were moving very slowly, and in the end froze completely and then the crash.

So my working theory is that the problem may be related to F#. So I have added the F# project folder to the exclude list in R# options. I also added the *.fs file type to the excluded files.

So far it's looking good. I have built several times, and editing F# code is smooth.

PS. Still looking good! :) Think I found the solution!

PPS. Also disabling R# for TypeScript files may be a good idea (or so I've heard).

2017-04-03

Things that may be confusing when moving to F#

I have been doing C# for quite a few years, and I have started using F# recently. I am only occasionally coding in F# so I tend to forget some things from one time to the other. So here's what I must remember:
  • The sequence of F# code files in Visual Studio matters!
    • To have access to a module or type from another module, that module must be declared before (higher up) the other module.
    • To move a module up, use Alt + ArrowUp.
  •  Indentation matters!
    • If some code is part of some enclosing element, that code must be indented.
Here's a great article:
http://connelhooley.uk/blog/2017/04/10/f-sharp-guide

2017-01-13

Free Code Improvement Alternatives

I really liked using ReSharper (JetBrains) in Microsoft Visual Studio.

There are also some other commercial tools like it:
- CodeRush (DevExpress)
- JustCode (Telerik)

The functionality that I found most useful was:
- Go To Implementation
- Find Usages
- Suggestions on code improvements
- Extract function

I think that ReSharper has become too expensive now, currently at a $299 first year subscription, then a bit lower on subsequent years. That's about 40% of the price of Visual Studio Professional, and you don't even buy the product - you subscribe.

So I am testing out this combo as an alternative:

Roslyn provides the Light Bulb for code improvements:
https://blog.tommyparnell.com/vs-2015-getting-resharper-experiance-without-resharper/

Several very handy tools:
- Productivity Power Tools

For more code improvement suggestions (pick one, not both):
- CodeCracker for C# (testing this now, looks good)
- Refactoring Essentials for Visual Studio*

I have also tested CodeMaid, but not just right now*.
I may enable it again at a later point in time though.
It looks like a very useful addition to Visual Studio.

Here are some related Stack Overflow questions:
http://stackoverflow.com/questions/2834439/what-are-some-alternatives-to-resharper
http://stackoverflow.com/questions/24542406/alternatives-to-resharper

*Enabling too many competing plugins may cause VS to run very slowly.

2016-08-05

Appear to browse from a different country

If you want to test what your website looks like for people in a different country, you might like to try this:

http://teleport.to/

Use at your own discretion.

2016-06-02

CS1056: Unexpected character '$'

When you get the message " CS1056: Unexpected character '$' " in runtime asp.net, it means that the front end file (html/aspx) is using a way of formatting strings which is called "string interpolation".
But to use string interpolation in your web frontend files you must put the following into your web.config file:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
  </compilers>
</system.codedom>

Source:
http://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view
 
Disclaimer:
The above worked for me, but may not work for all .NET Framework versions and/or ASP.NET versions.

2016-03-02

Catching mails

When developing a solution, sometimes it is nice to be able to catch mails and store them somewhere in stead of potentially sending them to real customers.

In web.config or app.config, there is a setting that can accomplish that for you:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\Mail"/>
    </smtp>
  </mailSettings>
</system.net>
 If you install an eml-viewer or maybe just a regular mail client, you can open and read the mails from the folder you specified.

Source:
http://stackoverflow.com/questions/567765/how-can-i-save-an-email-instead-of-sending-when-using-smtpclient