Thursday, February 9, 2012

Linq Except with Duplicate Members

List<string> popcapGames = new List<string> { "Zuma",

"Chuzzled",

"Bejeweled",

"Plants vs Zombies",

"Zuma",

"Peggle"

};

// I want all the elements except Chuzzled.

List<string> excludingChuzzled = popcapGames.Except(popcapGames.Where(x => x == "Chuzzled")).ToList();

// I expect excludingChuzzled to be: { "Zuma",

// "Bejeweled",

// "Plants vs Zombies",

// "Zuma",

// "Peggle"

// };

//

//

//

//

//

// but actually its: { "Zuma",

// "Bejeweled",

// "Plants vs Zombies",

// "Peggle"

// };

// What happened to my duplicate "Zuma"?

//

// I don't like Except any more.

var

My colleagues don't like the var keyword. I do.

Thursday, September 29, 2011

Safely invoking view-related methods from a controller

Now I've got this situation where I need to call view-refresher stuff from my controller. But the call happens to be inside a background worker. I get a cross thread exception: you can't access stuff that was created on the view thread.

Solution: Invoke it safely:

public void RefreshView()
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateDisplayStateCallback(this.RefreshViewInternally));
}
else
{
this.RefreshViewInternally();
}
}

(RefreshViewsInternally can now contain references to view components you wanted to actually refresh.)

What's that? You want to know how to make your call back method something with parameters? No problem:

public void RefreshViews(ReallyComplexObjectYouCreatedYourself myReallyComplexObjectYouCreatedYourself)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(this.RefreshViewsInternally), myReallyComplexObjectYouCreatedYourself);
}
else
{
this.RefreshViewsInternally(myReallyComplexObjectYouCreatedYourself);
}
}


Now, RefreshViewsInternally is allowed to have this signature: RefreshViewsInternally(ReallyComplexObjectYouCreatedYourself myReallyComplexObjectYouCreatedYourself);

Booya.

Monday, June 13, 2011

Modular Arithmetic

More a note to self than anything else:

Given x, return a value which is a multiple of y and "still sufficiently close to x". Surely there's a way of phrasing that second part more eloquently?

In C# code:

decimal TransformXToMultipleOfY(decimal x, decimal y)
{
decimal modResult = x % y;

if (modResult == 0)
{
return x;
}

return x+ (y - (x % y));
}

Sunday, May 10, 2009

Okay so I have the tools, now what?

Coming from a linux / vim environment where trying to get something to do what it's supposed to can at times be a painful experience I was surprised to see just how easy a package like Visual Studio can make your life. It seems as if a great deal of effort has been put into taking away the time consuming and frustrating tasks developers face.

The intellisense-thingy rocks. I know most packages offer this but I've never stopped to think just how much it can speed up what you're doing. Another feature that seems pretty cool are breakpoints. I expect the more I learn I'll see how powerful tools like these can be.

So where to now? I'll be doing alot of reading on msdn and another nice place to get some quick information is stackoverflow.

I think firstly I should get a better understanding of OO concepts and how it's actually used. Internet to the rescue!

Monday, April 6, 2009

Project 1: File Contribution, Tracking, Sharing, Collaboration, Synchronisation

On Friday, the 3rd of April at about 6 in the morning, it struck Robert that learning C# might not be a bad idea. We identified that the problem with learning a programming language or any software development methodology from scratch using online tutorials is that the usage examples don't really make any sense.

When people ask me what object orientation is, I use the classic example of the class "Person" and the object instantiation of "Person", "kareema" or "aPerson". I hear myself going on and on about how a Person needs a Name, an Age and a Sex and that kareema, the instantiation of the class Person needs particular values for those criteria.

Robert asks me, "When will I ever need that?" and it's clear to me that the only way to learn stuff like that is to need it. Incidentally, we do.

Enter the problem: My sisters and I have a network set up at home. We all share the same DSL internet connection and we all download data. Sometimes we download the same stuff without realising it. Bandwidth usage is a big thing for us because it happens to be relatively expensive where we live and we'd like to minimise it.

I know there are file synchronisation tools available for downloading and buying but we thought this would be a great opportunity to exercise skills, become more familiar with the development environment and identify needs for patterns and such the like.