Copy Multiple Items in Visual Studio 2019

As a developer, I have to copy and paste. A lot! Sometimes I need to copy several items from different locations and then paste them into the same file. This can be very tedious with the traditional Ctrl+C then Ctrl+V process. To help remedy this, I use Comfort Clipboard Pro, a very powerful clipboard manager. I realize for some this may be a bit of overkill or they don’t wish to spend the money on a solution.

Thankfully, Visual Studio 2019 has a feature which can help. Simply copy several items in a row using your preferred method — from the file menu or with a key command. Then place your cursor where you wish for the copied items to be pasted. Next, go to Edit in the file menu and select the Show Clipboard History option (alternately you can press Ctrl+Shift+Insert).

Select Show Clipboard History

This will display a popup list of the things you’ve just copied. Click one of the items in the list.

Displays list of clipboard items

The item you selected will be pasted into your file where your cursor currently sits.

Pasted item from clipboard

That’s it! Quick. Simple. It didn’t require any additional software. And it didn’t cost you anything. Hope that helps!

Copy Visual Studio 2017 configuration settings to Visual Studio 2019

Visual Studio 2019
Visual Studio 2019

Visual Studio 2019 shipped on April 1st. And one of the nice things about it is you can have it installed side-by-side with other version — like Visual Studio 2017. Most developers have spent some time selecting which features of Visual Studio they need installed. A nice addition to the new Visual Studio Installer is you can export your configurations from one version and import into another. So, if you’ve just installed 2019 and wish for it to be configured like your 2017 version, now you can! And it’s pretty straight forward.

Launch the Visual Studio Installer.

Click the Modify button next to your Visual Studio 2017 installation.
This will show you the current configuration. You can just close this modal if you don’t wish to make any changes.
Click the More drop down and then select Export configuration from your 2017 installation.
Choose a location on your system where you wish to save the configuration file.
You will then see all the current settings and give you one last change to make changes. Then click the Export button.
A confirmation message will display when exporting is successful.
Next, click the More drop down for your 2019 installation then click Import configuration.
Locate your configuration file you just exported.
It will display the configuration options which will be imported giving you a chance to make any changes. Then click the Modify button to make any changes to your 2019 installation.

And that’s it! The installer will download and install any needed components based on your older configuration file.

How to check which version of .Net Core you have installed.

Open a command prompt and enter the version command.

dotnet --version
dotnet version

This should return a version number. If you get something like the message below, .Net Core is not installed.

'dotnet' is not recognized as an internal or external command,
operable program or batch file.

You can also issue the info command to get a lot more information about your environment.

dotnet --info
dotnet info

If you want to actually look at the different .Net SDKs which you have installed, they can be found here on a Windows machine:

C:\Program Files\dotnet\sdk

JavaScript — Get the Currently Focused Element

In JavaScript, the currently active element is said to have Focus, which means it’s the element being acted upon. Like a form field in which you are typing is said to have Focus.

You can get the currently focused element with a simple reference on the Dom:

var currentElement = document.activeElement

This will return the element which has focus, or null if there is no focused element. It will let you have access to the whole element. You can get to any properties on that element like the Id or Name. The CodePen below is a simple example of using the document.activeElement

Example

See the Pen Get Focused Element by Chris (@cgreenws) on CodePen.

For more information:
https://www.w3schools.com/jsref/prop_document_activeelement.asp
or
https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement

JavaScript — How to use the Set object

JavaScript has an object called Set which will store any type with unique values — this can be a primitive value type (e.g. string, number, boolean, null, undefined, symbol) or even an object reference.

//create a new Set
var setA = new Set();
//use .add() to add types to the set
setA.add(6); // Set is [6]
setA.add(9); // Set is [6,9]

//or create a Set by passing it an iterable object
var setA = new Set([6,9]); // Set is [6,9]

Because a value in a Set may only occur once, it will eliminate duplicates when you add them.

//Eliminates duplicate values on creation
var setB = new Set([6,7,8,9,6,7]); // Set is [6,7,8,9]

//Doesn't add new values if they already exist in the set
var setB = new Set();
setB.add(6)// Set is [6]
setB.add(7)// Set is [6,7]
setB.add(8)// Set is [6,7,8]
setB.add(6)// Set is [6,7,8]
setB.add(9)// Set is [6,7,8,9]
setB.add(7)// Set is [6,7,8,9]

Use the .has() method to check if a value is in the set. It will return a boolean of True or False depending on if it finds the value.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true
setA.has(2); // false

The .delete() method will remove a value from the set. It will return a boolean value if it found the value in the set.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true
setA.delete(5); // returns true
setA.has(5); // false -- Set is now [6,9,3,8]
setA.delete(5); // returns false

You can also remove ALL the values in the set with the .clear() method.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true

A Set does not have a Length property but it does has a Size which will tell you how many values are stored in the Set.

var setA = new Set([6,9,3,5,8]);
setA.size; // return 5
setA.length; // returns undefined

To keep the Set similar to the Map object, values are stored as a key/value pair where the key is the same as the value. You can retrieve all the values in a Set using the .entries() method. This will return an Iterator of all entries in a format of [value, value].

var setA = new Set([6,9,3,5,8]);
var myEntries = setA.entries();

You can also just get the keys or values for a Set using the .keys() or .values() methods, respectively. Both return a new Iterator object. Since the value is used as the key in a Set, both methods ultimately return the same result.

var setA = new Set([6,9,3,5,8]);

var setKeys = setA.keys();
// or
var setValues = setA.values();

Because a Set is an iterable object, you can use the .forEach() method to loop over the values in a Set. You can pass a call back function in as a parameter which will get called once for each value in the set. It also has an option argument which can be passed that will be used for the this value for each callback.

var setA = new Set([6,9,3,5,8]);

setA.forEach(function writeValue(value) {
     console.log(value);
});

// 6
// 9
// 3
// 5
// 8

// example passing 'a' as the thisArg to the forEach
setA.forEach(function writeValue(value) {
     console.log(value + '---'+ this );
}, 'a');

// 6---a
// 9---a
// 3---a
// 5---a
// 8---a

If you just want to iterate over the values in a Set, you also have the option of using a For loop.

var setA = new Set([6,9,3,5,8]);

for (let item of setA) {
     console.log(item);
}
// or 
for (let item of setA.values()) {
     console.log(item);
}

// both output
// 6
// 9
// 3
// 5
// 8

You can easily convert from a Set to an array using Array.from() method.

var setA = new Set([6,9,3,5,8]);

var array1 = Array.from(setA); // [6, 9, 3, 5, 8]

//you can also use the Spread Syntax to create the Array of Values
var array2 = [...setA]; // [6, 9, 3, 5, 8]

One neat trick you can use Set for is to de-dupe an Array.

var array1 = [6,9,3,5,8,9,4,2,1,2,2]

var setB = new Set(array1);

var array2 = [...setB] //[6, 9, 3, 5, 8, 4, 2, 1]

// or you can simplify things like so
var array3 = [...new Set([6,9,3,5,8,9,4,2,1,2,2])]; 
//[6, 9, 3, 5, 8, 4, 2, 1]

Another is to use Set as a way to break a string apart to it’s individual unique letters.

var myString = 'Hello';

var charSet = new Set(myString); // ["H", "e", "l", "o"]
charSet.size; //4

//just keep in mind that case sensitivity counts
var myString2 = 'HeLlo';

var charSet2 = new Set(myString2); // ["H", "e", "L", "l", "o"]
charSet2.size; //5

There are a couple things to keep in mind. A set can only contain unique values. When you interate over a Set object, it returns the values in insertion order (how they go in is how they come out).

Objects can be a bit confusing at first compared to primitive types. For instance:

var obj = {'a': 1, 'b':2}
var obj2 = {'a': 1, 'b':2}
var mySet = [{'a': 1, 'b':2}, obj, obj2]

// mySet now contains
// [{'a': 1, 'b':2}, {'a': 1, 'b':2}, {'a': 1, 'b':2}]

How do I duplicate a line within Visual Studio Code?

If you want to easily duplicate a line of code in Visual Studio Code, you can do it with a simple keyboard shortcut. Just use the Copy Line Down (Ctrl+Alt+UpArrow) or Copy Line Up commands (Ctrl+Alt+DownArrow) — depending on where you want the new line inserted with respect to your cursor.

VS Code Short Cuts

You can view all the shortcuts like this in Visual Studio Code by going to the preference settings found at

File > Preferences > Keyboard Shortcuts

If you want to easily duplicate a line of code in Visual Studio Code, you can do it with a simple keyboard shortcut. Just use the Copy Line Down (Ctrl+Alt+UpArrow) or Copy Line Up commands (Ctrl+Alt+DownArrow) — depending on where you want the new line inserted with respect to your cursor.

JavaScript — Loop Over an Array with forEach() Method

You can use the forEach() method to loop over an array and execute a function on each element of the array.

var pets = ['dog', 'cat', 'monkey'];

pets.forEach(function(element) {
  console.log(element);
});

// expected output: "dog"
// expected output: "cat"
// expected output: "monkey"

For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Electron Error: Not allowed to load local resource

I’ve recently begun playing with Electron (the library that lets you build cross-platform desktop apps with web technologies). And not long into trying to hook up my default HTML page I received this error:

Not allowed to load local resource

I was certain I’d typed the line to load the URL just like the tutorial suggested. I searched around and was having trouble finding out why my app wasn’t working. Finally, in a reply buried in the comments was the answer. I was using single quotes and what I needed was the back tick for my value passed.

I had this

mainWindow.loadURL('file://${__dirname}/countdown.html');

But needed this

mainWindow.loadURL(`file://${__dirname}/countdown.html`);

It’s an incredibly easy thing to not notice, but because I was dynamically swapping out the ${__dirname} variable, I had to use the back ticks (`) and not the single quote (‘).

Hopefully that will help other Electron beginners experiencing the same issue.

What is the JavaScript ternary operator?

The ternary operator is a JavaScript Operator which will return one of two values depending on a conditional supplied. Basically, you supply any conditional which will evaluate out to true or false and it will return one of the two expressions. It is set  up as such:

condition ? expression1 : expression2

If the condition evaluates true, then expression1 is returned. If the condition evaluates to false, then expression2 is returned.

Example

var pet = {
    name: "Spot",
    species: "Dog"
};
var soundMakes = pet.species === "Dog" ? "woof" : "meow";
console.log(soundMakes);
//expected output: "woof"

You can use multiple conditions when evaluating. And you can put functions in the expression section to run complex code.

Example

var aDog = true;
var myPet = false;

aDog && myPet ? function() {
    alert("It's my dog");
    console.log('Give the dog a treat');
} ()
:
function () {
    alert("Not my dog");
    console.log('Walk away quickly');
} ();

For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator