Sunday, October 25, 2015

Minimizing the distance between class property declaration and initialization in auto-backed properties

In C# you can do:
public class Foo1
{
    public string Bar {get; set;}
}

The compiler automatically creates the backing field for the Bar property.
Before this auto-backing syntax sugar you would do this:

public class Foo2
{
private string bar = "";
public string Bar
{
   get {return bar;}
   set {bar = value;}
}
}

Ouch, my fingers hurt from all that extra typing.

This is some nice syntax sugar. However, there's a problem with it. When I heard about this feature and started using it, I incorrectly assumed that the backing field would be initialized to something. Nope! So Foo1 != Foo2. To make Foo1 be equivalent to Foo2 you'd have to initialize bar in the constructor.
public class Foo1Again
{
    public string Bar {get; set;}
    public Foo1Again()
   {
        Bar = "";
    }
}


The problem with this is you might be used to typing the backing field, and incorrectly assume that it's initialized for you. That's when Null Reference exceptions start happening. So if you decide to use auto-backed properties, you need to remember to init in the constructor. Ouch... I'd rather stick with typing out the backing field myself, so I don't randomly forget to initialize the field.

However, in C#6 they solved this, and you can now initialize the property inline with the declaration!!
See this: http://stackoverflow.com/a/169872/1538717

public class FooCharp6
{
    public string Foo {get;} = "";

}
   





Friday, October 23, 2015

PInvoke for finding all MSI locations of installers with a specific UpgradeCode

class Program { /* References http://www.pinvoke.net/default.aspx/msi/MsiEnumRelatedProducts.html http://stackoverflow.com/questions/3475793/how-to-find-program-location-in-registry-if-i-know-msi-guid */ static void Main(string[] args) { string upgradeCode = "{CC6ED12E-5384-4C17-A293-D6D19371A5E8}"; StringBuilder sbProductCode = new StringBuilder(39); int iIndex = 0; while (0 == MsiEnumRelatedProducts(upgradeCode, 0, iIndex++, sbProductCode)) { Int32 len = 512; StringBuilder builder = new StringBuilder(len); MsiGetProductInfo(sbProductCode.ToString(), "LocalPackage", builder, ref len); Console.WriteLine(builder.ToString()); } Console.ReadLine(); } [DllImport("msi.dll", CharSet = CharSet.Unicode)] static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len); [DllImport("msi.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern UInt32 MsiEnumRelatedProducts(string strUpgradeCode, int reserved, int iIndex, StringBuilder sbProductCode); }

Pinvoke.net references

PInvoke reference site: pinvoke.net

PInvoke Visual Studio Extension: https://visualstudiogallery.msdn.microsoft.com/9CA9D544-05D2-487B-AB49-31851483C1CC



Thursday, October 22, 2015

Paste stopped working in an RDP session

Do these steps on BOTH your computer and the computer you're RDPing to
     1. Open Task Manager
     2. Click Processes
     3. Find rdpclip.exe > right-click > End Process (this might not be running)
     4. Start > Run > rdpclip.exe


Reference
http://web.archive.org/web/20100217022822/http://blogs.msdn.com/rds/comments/1089729.aspx 


Sunday, October 4, 2015

Playing Starcraft on Windows 7

Fix the colors
1. Create a batch file
2. Put this in it
taskkill /f /IM explorer.exe
<PUT YOUR INSTALL PATH HERE>StarCraft\StarCraft.exe
explorer.exe

exit

3. Execute the batch file anytime you want to play.

note: Without explorer open you can still alt-tab between programs. So i recommend opening a cmd prompt + a web browser in case you need to look something in while having Starcraft open


The latency of the game creator is too high1. Open your router configuration page.
2. Find Port Forwarding (this could be called Applications and Gaming as well) - it will depend on your router model
3. Add port range 6112-6119 TCP and UDP


There was an error in this gadget