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)
                    dto.Rates.Remove(rate.Key);
            }

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];
        keysToNuke.Add(key);
    }
}
foreach (string key in keysToNuke)
{
    colStates[key] = 0;
} 
 
 
Reference:
http://stackoverflow.com/questions/1482234/hashset-iterating-while-removing-items-in-c-sharp 
http://stackoverflow.com/questions/1070766/editing-dictionary-values-in-a-foreach-loop 

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."

Reference:

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.

http://h10025.www1.hp.com/ewfrf/wc/softwareCategory?os=4158&lc=en&cc=us&dlc=en&sw_lang=&product=311255#N183

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.

Reference:
http://www.gamefaqs.com/boards/997614-nintendo-3ds/68059469

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!

Reference:
http://stackoverflow.com/questions/14824174/cannot-control-startup-projects-in-visual-studio

Monday, November 25, 2013

Alternative to Window Gadget for Windows 7

Due to some "serious vulnerabilities" with Gadget. Microsoft decided to stop supporting Windows gadgets. For those who use Windows 8, they should be fine because the apps will be alternatives.

For Windows 7, consider Rainmeter (@ http://rainmeter.net/cms/). The installation was pretty straight forward. The app doesn't seem to hog my system resource at all. The downside is that the default "skins" doesn't have all the replacement for my usual gadgets.

There should be more options to choose from, I guess I just need to spend more time on it.