Tahir Hassan's Blog

My Technical Notes

Thursday, 13 July 2017

Visual Studio 2017: An Alternative to the DataTable Visualizer

In many environments, the DataTable Visualizer does not work because its DLL must reside in the "My Documents" folder. Unfortunately, if you do not have control of your computer, this folder could be on the network, which would stop the visualizer from working.

A simple alternative is to use PowerShell's `Out-GridView` within C#. The following class `_PS` defines the extension method `OutGridView` which will invoke `Out-GridView`:


using System;
using System.Collections;
using System.Data;

public static class _PS
{
    public static void OutGridView(this DataTable t)
    {
        t.Rows.OutGridView();
    }

    public static void OutGridView(this IEnumerable t)
    {
        using (var ps = System.Management.Automation.PowerShell.Create())
        {
            ps.Commands.AddCommand("Out-GridView");
            var result = ps.Invoke(t);
        }
    } 
}

For instance, if we had a `DataTable` `dt`, then in the Intermediate/QuickWatch/Watch window, you can evaluate:


dt.OutGridView()

To use the above code, install the `System.Management.Automation` NuGet package.

Calling PowerShell from C#

First add the `System.Management.Automation` package:


Install-Package System.Management.Automation

The following code sample doubles a sequence of integers `1, 2, 3`:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ps = System.Management.Automation.PowerShell.Create())
            {
                var foreachCommand = new System.Management.Automation.Runspaces.Command("ForEach-Object");
                foreachCommand.Parameters.Add("Process", System.Management.Automation.ScriptBlock.Create("$_ * 2"));

                ps.Commands.AddCommand(foreachCommand);

                var result = ps.Invoke(new[] { 1, 2, 3 });
            }
        }
    }
}

Wednesday, 12 July 2017

PowerShell `Out-GridView` Example for a DataTable

Given some sample C# code:


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
    public class XXClass
    {
        public static System.Data.DataTable CreateSampleDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Forename", typeof(string));
            dt.Columns.Add("Surname", typeof(string));

            {
                var row = dt.NewRow();
                row["ID"] = 1;
                row["Forename"] = "Tahir";
                row["Surname"] = "Hassan";
                dt.Rows.Add(row);
            }

            {
                var row = dt.NewRow();
                row["ID"] = 2;
                row["Forename"] = "Steven";
                row["Surname"] = "Gerrard";
                dt.Rows.Add(row);
            }

            {
                var row = dt.NewRow();
                row["ID"] = 3;
                row["Forename"] = "Luis";
                row["Surname"] = "Suarez";
                dt.Rows.Add(row);
            }

            return dt;
        }
    }
}

Setting `$code` to this text, you can run the following code to add this type:


Add-Type -TypeDefinition $code -ReferencedAssemblies System.Data, System.Xml

And you can use `Out-GridView` to view the output of the above class's method:


[ConsoleApp6.XXClass]::CreateSampleDt() | Out-GridView

Thursday, 6 July 2017

Programming Verbs: Populate

The verb `Populate` means:

A method `PopulateX()` populates an object that is named `X` with data appropriate for `X`. The underlying data source is understood from the method's context. Generally these methods do not return anything, and do not accept parameters.
For example, if you had a method:

public void PopulateStudentGrid() {
    // ...
}

The `StudentGrid` would be populated with data from a source that is understood from the context.

It is necessary to give such a method a documentation comment because those unfamiliar with the code will not know what data you are populating `X` with or where it comes from.

Programming Verbs: Get and Set

The following are verbs/nouns synonymous with `Get` and `Set`:


| Origin         | Get                      | Set          |
|----------------+--------------------------+--------------|
| General        | read                     | write        |
| General        |                          | put          |
| OO             | get                      | set          |
| git            | pull, clone              | push, commit |
| Data warehouse | extract                  | load         |
| General        | out                      | in           |
| General        | output                   | input        |
| Programming    |                          | return       |
| General        | query                    | command      |
| HTTP           | GET                      | POST, PUSH   |
| General        | emit*                    | absorb*      |
| General        | produce*                 | consume*     |
| General        | producer*                | consumer*    |
| General        | open, subscribe, consume |              |
| Programming    | access                   | mutate       |
| General        | export*                  | import*      |
| General        | retrieve                 |              |
| General        | function*                | action*      |
| General        | receive                  | send         |

The starred items are words from the point of view of the object being gotten or set.

Friday, 30 June 2017

Viewing Hidden Passwords in Windows 7

To view hidden passwords in Windows 7, download PSExec, unzip to a folder, and run this from the command line as admin:


PsExec.exe -i -s rundll32 keymgr.dll,KRShowKeyMgr

Sources

Configuring VirtualBox

Click on File → Preferences. In the General tab, set "Default Machine Folder" to a more appropriate location.

These settings are stored `VirtualBox.xml` in `%USERPROFILE%\.VirtualBox`.