Author: carstent

Visual Studio 2017 Editor new line wrapping

We all have our favourite settings when it comes to setting up Visual Studio, and the editor is quite often set up differently for a group of developers working together. I like having the opening brace ({) on the same line as the class, struct or whatever the brace signals the beginning of.

I like this:

class {

as compared to this:

class {

I frequently use the key combination Ctrl+K, Ctrl+D to format my document after having typed some code. I dislike it, if the formatting is not to my liking. It should therefore not come as a surprise, if I tell you that while working on a new UWP app, in which I had copied across some code from a Template10 project template, the key combo seemingly didn’t work anymore. After some investigation, including opening an existing Console app, in which the key combo did work, I took a look at this code:

namespace Template10.Views
{
    public sealed partial class SettingsPage : Page
    {
        readonly Template10.Services.SerializationService.ISerializationService _serializationService;
 
        public SettingsPage()
        {
            InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Required;
            _serializationService = Template10.Services.SerializationService.SerializationService.Json;
        }
 
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var index = int.Parse(_serializationService.Deserialize(e.Parameter?.ToString()).ToString());
            MyPivot.SelectedIndex = index;
        }
    }
}

Because of the Page class, from which the class shown, SettingsPage, is derived has a notification stating “Base type ‘Page’ is already specified in other parts”, the class/type name Page is greyed out. This effectively blocks for the document formatting placing the opening brace { on the same line as the class declaration. If I remove : Page, and press the key combination Ctrl+K, Ctrl+D, this is what I get.

namespace Template10.Views {
    public sealed partial class SettingsPage {
        readonly Template10.Services.SerializationService.ISerializationService _serializationService;
 
        public SettingsPage() {
            InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Required;
            _serializationService = Template10.Services.SerializationService.SerializationService.Json;
        }
 
        protected override void OnNavigatedTo(NavigationEventArgs e) {
            var index = int.Parse(_serializationService.Deserialize(e.Parameter?.ToString()).ToString());
            MyPivot.SelectedIndex = index;
        }
    }
}

 

Advertisements

You should only open projects from a trustworthy source prompt

In Visual Studio 2017 and previous versions, if you attempt to open a project on a network share, such as one located on a Mac, that helpful, but rather annoying message prompts you. There’s a quick fix for that;

  • Open Visual Studio, on the Tools  menu, click Options.
  • In the Options dialogue box, on the list on the left, expand Projects and Solutions and click General.
  • Uncheck Warn user when the project location is not trusted, and click OK.

Simple…

Visual Studio 2017 Create Strong Name Key File

When trying to create a strong name key file for your project, in this case a UWP project, in Visual Studio 2017, and probably previous versions too, you may get an error message, once you click OK in the Create Strong Name Key dialogue box.

CreateStrongNameKeyWindows-VS2017

The operation could not be completed message box.

TheOperationCouldNotBeCompletedDialog

It turns out that all I needed to do, was to run Visual Studio as an administrator, and it worked. It’s the actual signing with the password that requires this, as you can create a key file without a password, and the signing will work without running VS as an administrator.

MS Band 2 battery dead

I’ve been using MS Band for a few years, initially the original Band and then the MS Band 2. I’ve had the latter replaced twice, but now that device is no longer able to be charged, i.e. the battery is fucked, and of course I’m out of warranty. I love my Band and I wear it all the time, it’s a fantastic little device and I pray to the MS Gods they will bring out a new and even better one sometime soon.

However, what do I do in the meantime, I really need a device for when I go running or walking… Any ideas?

C# Comments

This is another of my pet peeves when looking at other people’s code. If you add a comment, using the double forward-slash //, then add a freaking space after // to make it stand out from code that has been commented out. It pisses me off right, left and center to see this:

//TODO blah, blah

…or this:

//Here is where you add...

Why not just do this, so flipping simple:

// TODO:

This will avoid any potential mistake when glancing over the code, where the code below has is clearly commented out code, and the first examples above are not:

//var numDays = int.Parse(Console.ReadLine());
//var numPeople = 5;
//var numPeopleShared = 0;

Do feel free to share your opinion and experience on the same.

OutOfMemoryException in online test

I did an online test the other day, and while the code I had put together did work and passed 3 of the 4 testcases, the failing testcase throw an OutOfMemoryException. I was writing the code in Visual Studio 2017 and copying it to the web-based IDE on TestDome. Now, since I don’t know what the testcase did, I can only assume that it added a very large number of int’s to an array, for which I was to find the kth most common integer in the array. This is my code:

public static int KthMostCommon(int[] a, int k) {
    return a.GroupBy(v => v)
        .OrderByDescending(g => g.Count()).ElementAt(k - 1).Key;
}

I only got a 66% score for that task, but it dawned on me, that I should have tried using a byte array instead. Trying it with the following call to the method and obviously changing the method signature to accept a byte array instead of a an int array, worked a charm.

var t = Enumerable.Repeat<byte>(0, 1000000000)
    .Select(i => (byte) randNum.Next(minValue, maxValue)).ToArray();
var x = KthMostCommon(t, 2);

What would you have done, not knowing the number of integers that was in the array?

UPDATE:

I obviously couldn’t have changed the method signature as suggested in the actual test, as that would have caused all unit tests to fail. Another option is to pass the int array and convert to a byte array, and then disposing of the int array before processing the byte array.

var bytes = new byte[a.Length * 4]; 
for (var i = 0; i < a.Length; i++)
     Array.Copy(BitConverter.GetBytes(a[i]), 0, bytes, i * 4, 4);
a = null; 
GC.Collect();

Mind you, it would probably be better changing the processing alltogether… 🙂

Is a string an Anagram of another string?

How do you check if a word is an Anagram of another word? It’s reasonably simple, although I’m sure this can be simplified.

bool IsAnagram(string s1, string s2) {
    if (s1.Length != s2.Length) return false;
 
    var s1C = s1.ToCharArray();
    var s2C = s2.ToCharArray();
    Array.Sort(s1C);
    Array.Sort(s2C);
 
    return s1C.Equals(s2C);
}

Is a string a Palindrome?

A string is a palindrome if it reads the same, forwards and backwards, but how can you check in C#? There are at least hundreds of possible solutions for how to achieve this, but I think this is an easy one.

var originalString = "jjkjhkjhkj";
var reverseString = new string(s.Reverse().ToArray());
// Check for existing palindrome
if (s == reverseString)
    ...

Is a string a Pangram?

When is a string a pangram? Is actually fairly easy to check and beow you can see one implementation in C#.

var charsFound = new bool[26];
var s = Console.ReadLine().ToUpper();
 
// We need at least 26 characters (English alphabet) in the string...
if (s.Length >= 26) {
    
 
    foreach (var c in s)
        if (c >= 'A' && c <= 'Z') {
            // Uppercase ascii character codes starts at decimal index 65
            var index = c - 65;
            // Already marked, continue loop...
            if (charsFound[index]) continue;
 
            charsFound[index] = true;
        }
}
 
// Did we find all 26 chars?
Console.WriteLine(charsFound.Where(c => c).Count() == 26 ? "pangram" : "not pangram");

Start Page in Visual Studio 2017

What the heck happened with the Start Page in VS 2017? Well, not the Start Page as such, but how do you open it, after closing it manually or opening/creating a project or solution? In prior versions you could access the Start Page menu item on the View menu, and going further back over the years, I do believe it ws accessible from the Help menu at some point. In any case, it is now residing on the File menu…

VisualStudio2017StartPage