Saturday, October 23, 2010

Quick Tip: Windows key combination to launch a program from the start menu with adminitrator rights.

Scenario
You need to start a program from the start menu with adminitrator rights. You already know how to do it the long way, by right-clicking and drilling into the properties for the shortcut, but you wish there was a faster way.

How
Open the start menu and type the name of the program just as if you were starting it normally.

Hold down control+shift when you hit enter.


Yay, now the program launches with adminitrator rights!

Friday, October 22, 2010

Foo for Thought: 'Faking' a private readonly property in C#.

Scenario
We find ourselves wanting to implement a public readonly property.

But
We are annoyed by the fact that the internal name is different than the external.

public class C
{
   public C()
   {
      myProp = "V";
   }
   private readonly string myProp;
   public string MyProp { get { return myProp; } }
}

If you have an interface in place like this:

public interface IC
{
   string MyProp { get; }
}
public class C
{
   public C()
   {
      MyProp = "V";
   }
   // ---------------------------------------------------
   // Then as far as the rest of your code is concerned,
   // this is basically the same as if C# supported 
   // public readonly properties.
   // ---------------------------------------------------
   private readonly string MyProp;
   string IC.MyProp { get { return this.MyProp; } }
}
EOF

Hack-A-Riffic Batch Script Pays Off

Why
I'm writing code that uses a REST API and as project input I have a set of URLs to XSD schema files. I use svcutil.exe to download them from the command line and xsd.exe to generate XML serialization code to make it a breeze retrieving and posting valid requests.

I ran into a snag when the long filenames generated by svcutil combined with xsd's behavior that concatenates its input files to form the name of its (one) output file.

So, I took the opportunity to break out the .BAT file. It's been years, but I've done it before, I can do it again, right? (right)

Long story medium sized, I learned the same few things that I had forgotten a few years ago when it had been a few years since I'd done up a .BAT (See the Notables near EOF).

I wasn't terribly proud of my concoction at first, but the next day when I had to add another XSD schema to my code, and it took me all of 13 seconds, I decided I was, in fact, quite proud.

Here it is, code that first bravely deltes all the XSD and CS files in the current directory, then works from a list of names that get plugged into a URL as part of the svcutil command which downlaods each XSD schema one by one. At this point, the files have really long names based on the URL they came from. If xsd.exe got all of them together, it could (would, will, did) exceed the maximum length allowed by the tool (or the O/S I don't know which, take your pick) --> thus! --> my seasoned hacking brain sprung forth the idea to get the names of the recently downloaded files using the special form of the for loop that acts on directory listings (now you really see the roots of bravery) and renamtes them to 1, 2, 3, etc.

Problem solved :)

I *know* there's a fine chance this entire exercise was unnecessary due to there being a more sensible way of achieving the same goal. Yet, I read the help for xsd and svcutil twice, and nothing came to mind. I'll be very happy to have someone tell me my prized script is pointless in exchange for schooling me proper.

cls 
echo OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
del *.xsd *.cs 
for %%X in (resourceList campaign campaignGroup affiliate affiliateGroup stats payout) do (  
 svcutil.exe /nologo /t:metadata https://da-tracking.com/apifleet/xsd/1_0/%%X.xsd 
) 
set C=/classes /n:DAgents.DirectTrack.Rest 
set N=1 
for %%X in (*.xsd) do (  
 copy %%X !N!.xsd  
 del %%X  
 set /a N=!N!+1 
) 
for %%X in (*.xsd) do (  
 set C=!C! %%X 
) 
xsd %C% 
echo ON 
Notables
  • Use echo OFF/ON to supress extra garbage on the screen while script is running.
  • W.S.T. loop variables, use a single % if entering from DOS prompt and %% if coding a .BAT
  • To code logic that builds up a string via successive concatenation in the body of a loop, use 'SETLOCAL ENABLEDELAYEDEXPANSION' in combination with yet another form of variable referencing as in !N!
  • The for loop has handy features like the ability to loop over the results of a directory list.
  • To do math, use 'set /a'.

EOF

Wednesday, October 20, 2010

Subtlety: When the location of using makes a difference for importing namespaces in C#.

Scenario
You've noticed C# code that imports namespaces with the 'using' keyword in two slightly different syntactic contexts.

  1. At the top of the source code file.
  2. In the body of another namespace.

Understand by Example
namespace My {
  public class C { }
}

namespace My.Foo {
  public class C { }
}

// C from My
namespace My.Bar {
  public class Bar { private C instance; } 
}

// C from My <-- this might be counter intuitive
using My.Foo;
namespace My.Bar {
  public class Bar { private C instance; }
}

// C from My.Foo
namespace My.Baz {
  using Foo;
  public class Baz { private C instance; }
}
EOF

Tuesday, October 19, 2010

Quick Tip: Using Visual Studio Command Prompt Programs from Windows PowerShell

Scenario:

You use the Visual Studio Command Prompt to invoke programs like cs.exe and xsd.exe and you want to invoke those programs from within PowerShell.

Ups:

You can use the right-click to paste short cut that is not available in a regular DOS shell.

Downs:

This procedure explcitly invokes the DOS interpreter causing you to leave the PowerShell immidiate mode environment.

Alt:

It is possible to create a powershell script that causes the same environment changes programatically, but it's more complicated.  See here.

Steps:

Right click on the shortcut (usually found in the start menu) that you would use to launch the Visual Studio Command Prompt.

In the properties window that appears, in the Shortcut tab, locate the value of Target.  It should be something like:

%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" amd64
You may replace amd64 above with one of:

   x86 | ia64 | amd64 | x86_amd64 | x86_ia64

Launch PowerShell.

Use copy and paste (in powershell right-click is a short cut to paste text from the clipboard)  to construct a command similar to:

cmd /k 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat' amd64

EOF

(IN PROGRESS) An instructional walk-thru.

Learning the MVP pattern is easier by example.

As such, the following table is only to plant a tiny seed in your mind.  Clarity will come by working through examples and then experimenting on your own.

Part Purpose
IModel Data
IView UI
Presenter Intermediary between View and Model.

Scenario
You have been asked to write a program that lets its user pick two currency types and convert an amount from one to the other.

Model
You define an enumeration type to represent the supported kinds of currency.

public enum CurrencyKind
{
   USD, // U.S. Dollar
   EUR, // Euro
   AUD // Australian Dollar
   // Additional currencies may be added here.
}

You define an interface that specifies the functionality required to convert one currency to another.

public interface IModel 
{
   double ConvertCurrency(
    CurrencyKind from,
    CurrencyKind to,
    double amount);
}

View
You define an interface that specifies the movement of information to and from the UI.  Note the use of properties and events to model the direction of data flow. If you're wondering what exactly it is that the data will flow between, dont worry, that's the right question. Stay tuned. (Hint: if you guessed "View and Presenter" you were right)
public interface IView
{
   CurrencyKind FromCurrencyKind { get; }
   CurrencyKind ToCurrencyKind { get; }
   double ConvertedAmount { set; }
   event EventHandler ConvertCurrency;
}

Model
You do not want to worry about the details of currency conversion right now so you make a temporary implementation of the model that will suffice.

public class TemporaryModel : IModel 
{
   double ConvertCurrency(
     CurrencyKind from, CurrencyKind to, double amount)
     {
       // Hard-coded conversion.
       // For now the result is always twice the amount.
       return amount * 2;
     }
}

Presenter
TODO: stub the presenter

View
TODO: Implement first version of concrete View.

Main Program
TODO: Create a driver program to create the objects and get things going.

Test from windows live....

another...

test.