I just discovered two real highlights in C# 6.0. Some may call them syntactic sugar, but for me they are a great help keeping my source code clean and tidy. Continue reading „C# 6.0 – my highlights“
Well, your learn something new every day. Today I learned that there’s actually a neat class in the .NET Framework that’s called TextFieldParser and resides in the Microsoft.VisualBasic.FileIo namespace.
It helps you parse fixed-length or field-separated text files and is documented here.
Note to self: To calculate the average without actually storing all the values, you can go this way:
avg_new = avg_cur - (avg_cur - value) / num_values
So all you really need is the number of values you’re calculating the average for.
I recently had the task to send some JSON to a REST API. After deciding to use Newtonsoft.JSON I wondered how to nicely create the JSON structure I needed. Continue reading „Creating JSON in C#“
Previously I wrote a post about making sure the Windows PATH environment variable doesn’t exceed a certain length.
I found out it’s possible to use other environment variables to help you shorten the path. For example, you could store the C:\Program Files (x86) prefix in a new environment variable name PA1 and the C:\Program Files prefix in PA2.
You can then write %PA1%\Microsoft\… instead of C:\Program Files (x86)\Microsoft\… in your PATH variable.
NOTE: The new environment variables must be parsed by the system before the PATH variable, which also means they must appear before PATH when sorted alphabetically.
Sometimes you need to get a DATE that is the first of the current month in Microsoft Dynamics NAV. This is not as trivial as it may sound, but the following command does it:
This date formula gets the current month (that’s always the last day of the month in NAV), adds a day (so we are on the first of the next month) and subtracts a month. Here you go.
In case you need an overview which T-SQL type maps to which C# type, here’s a nice page I found:
For a couple of days I’ve had major trouble with my work notebook. After installing new Visual Studios (2012 and 2013), all of a sudden various system software stopped working.
For example several applications could no longer be started using the pinned icons in the task bar. Also, when trying to invoke some system dialogs in the Control Panel I got error messages like %windir%\system32\xyz.exe was not found. Or the strange fact that all the entries in the Start Menu lost their symbols…
After some days of much cursing and swearing, several attempts of restoring previous Restore Points, etc., I got a hint that solved my problem:
The content of the PATH environment variable may not be longer than 2048 characters! If it is, neither the PATH variable, nor any other %..% system variable will work.
I shortened the path, rebooted, problem solved. Welcome to Windows 7, where the PATH variable is still limited in length…
Sometimes you need to round DateTimes in T-SQL to a certain DateTime component. For example, you need to truncate the minutes and seconds and only keep the date and the hour. To do so, you can do this:
SELECT DATEADD(hh, DATEDIFF(hh, '20000101', getdate()), '20000101')
This turns 20120101 13:59:59 into 20120101 13:00:00. Please note that the comparison date 20000101 is totally arbitrary. You can use any day before the date you’re truncating.
Sometimes you need to return from a stored procedure the results of a SELECT as a comma-separated string.
Example: The SELECT returns:
But you want the stored procedure to return:
Hello, World, It's, Me
Instead of looping with a cursor you can solve this pretty elegantly like the following:
The AND Field IS NOT NULL at the end of the WHERE-clause is important, because otherwise the result would always be null if one of the values was null (because something + NULL = NULL).