Monday, March 24, 2014

How to remove HashSet or Dictionary element in foreach loop in C#

If we want to remove an element from hashset inside the loop, we cannot use the following approach

            //Remove the rate that has 0 value
            foreach (var rate in dto.Rates){
                if (rate.Value == 0)

That is because once the key gets remove, the property is no longer immutable.

The fix to this is to use RemoveWhere instead:

hashset.RemoveWhere(s => s == "somestring");
For dictionary, use this instead since it does not have RemoveWhere() method
List<string> keysToNuke = new List<string>();
foreach(string key in colStates.Keys)
    double percent = colStates[key] / TotalCount;    
    if (percent < 0.05)
        OtherCount += colStates[key];
foreach (string key in keysToNuke)
    colStates[key] = 0;

Sunday, January 5, 2014

Install Intel Graphic Card Driver on Surface Pro 2 (Windows 8.1)

For some reason Windows 8.1 will not let me install Intel's latest graphic card driver using their auto detect download, which return an exe file to the web browser.

So I ended up having to download the zip version instead and then follow the instruction by Matt_NZ from Reddit

"[–]Matt_NZ   ago
You need to download the zip version of the Intel drivers. Extract the zip file then go to Device Manager, find the graphics card in the list and go to its properties. Choose to update the driver and then pick "Browse my Computer" and then "Let Me Pick from a list". On the next screen, choose Have Disk and then browse to the location where you extracted the Intel drivers. Go into the Graphics folder within that and then choose Ok. You should then get a list of drivers to choose from in the original window you clicked Have Disk in - choose Intel HD Graphics 4400 and continue through the wizard."


Unable to install HP Photosmart 5510 on Windows 8.1 device.

Turned out we can't just add device via control panel.

Instead we'll need to download the driver from HP website.

Tuesday, December 24, 2013

Cannot Connect eShop after link 3DS account with Wii U

If you have issue connecting to the eShop after linking up 3DS account with Wii U, tried to turn off the system. Take out the SD card, put it back. Then try again.

The problem should go away after that.


Friday, December 20, 2013

Got "Object reference not set to an instance of an object." when trying to open the property of VS 2012 solution.

I tried to set the startup projects on VS 2012 solution and got the following pop up message: "Object reference not set to an instance of an object."

Turned out the issue caused by VS2012 extension is not up to date. So I had to go to TOOLS -> Extensions and Updates... and update all the tools I had in there (it seems older version of NuGet was the main problem).

After the restart, I was able to set up the startup project again. Phew!