Sorting a List


I came across a different requirement today, I need to sort some objects in a certain order that wasn't alphabetical or numerical.  I looked towards SQL for a few minutes and found zilch that could help me with ORDER BY shinanigans.  I could have done multiple queries and then merged the results, but that would be pretty messy.  I ended up using Sort(), which I used before, but never to do anything more than a simple sort.

Consider that I have the following Employee objects with a Name and Position property like:

Joe, President

Frank, Vice President

Bob, Developer

that need to be sorted in that order.  Obviously sorting alphabetically wouldn't work so what to do?

Sort to the rescue!

Employees.Sort(CompareEmployeesByPositionInCompany);

with CompareEmployeesByPositionInCompany looking like:

private static int CompareEmployeesByPositionInCompany(Employee x, Employee y) {
            int xSortValue = GetSortValue(x.PositionInCompany);
            int ySortValue = GetSortValue(y.PositionInCompany);
            const int before = -1;
            const int same = 0;
            const int after = 1;

            if (xSortValue == ySortValue) {
               return same;
            }
            if (xSortValue < ySortValue) {
               return before;
            }
            return after;
         }

private static int GetSortValue(string value) {
            if (value == "President")  return 1;
            if (value == "Vice President") return 2;
            if (value == "Developer") return 3;

}

author: Willie | posted @ Tuesday, August 12, 2008 2:08 PM | Feedback (0)

Update Table with column and default value


When updating a table with a new column first I'd do something like:

alter table myTable add isGreat bit null

then fill that column with a value like so:

update myTable set isGreat = 0 where IsConfirmed IS NULL

Turns out there's a shortcut to that sucker:

alter table myTable add isGreat bit null default 0 with values;

author: Willie | posted @ Thursday, August 07, 2008 12:11 PM | Feedback (0)

Earthquake? I'm coding biatch


I had just gotten done talking to the boss about getting Resharper 4 and doing and upgrade to VS 2008.  I walked back to my office and was in the doorway when the "medium" one hit.  Having never been in a earthquake larger than one that shakes me about a tad it was weird seeing the floor shake around.

A train was going by right outside the office, so at first I thought that it was just the train, but then a huge bump came and I was wondering if the ceiling was going to come down.  I was going back to code, but then thought, "well I'm supposed to stay under a door or something and here I am already."

I called out to the other people in the office to see if everything was alright.  My co-worker though didn't seem phased in the slightest.  In fact, he looked up, then went back to coding.  Now that's some intense concentration!

author: Willie | posted @ Thursday, July 31, 2008 1:44 PM | Feedback (0)

NCover is alive again?


Looks like 3 peeps brought NCover back from the grave of sorts.  While the product never died, it did go commercial which essentially left us poor peoplies with no coverage tool.   Glad to see it resurface in the open source area again.

author: Willie | posted @ Wednesday, July 30, 2008 11:19 AM | Feedback (0)

Server Client and back again


Wouldn't it be totally possible to have an application have both a server and client piece?  Both would contain a list of each connected IP addresses to the each of the connected machines to a central server machine.  This would allow more of a push technology to populate a sceen, and if a server went down it probably wouldn't be a big deal because then a client app could just take over server duties anyway.

author: Willie | posted @ Monday, July 28, 2008 3:06 PM | Feedback (0)

Why would you have a custom server to pass data from a database?


I have a client-server application that is going from the client to the server.  From there the server is getting data back from SQL Server and then through the serialization process sending it back to the client.  Why not just go directly to the SQL Server?  What could be the benefit.  The SQL Server and Server module are both hosted in the same area so I don't think it's security.  Maybe it's just proprietary stuff... but I'm not sure.

author: Willie | posted @ Monday, July 28, 2008 3:01 PM | Feedback (0)

Hey buddy, wanna make 0 dollars?


As I get older I realize that my instinct are pretty much dead on about 90% of the time.  When warning flags shoot up, I really, really, reeealy need to start paying attention to that inner voice that is telling me, "Somethin fishy going on here."

I get a call on Sunday at 7PM.  A person who was a friend of a friend tells me that he needs a "master page layout" done, but that it has to be done by the next day.  There's 400 bucks if I'm interested.  The catch is, that it has to be done by the next morning.  I figure, 400 bucks for just a master page, why the f not?

I killed my Sunday fun short and got home around 10:30PM.  Usually, this is when I'd make some preperations for bed, and for the Monday instead.  I steeled myself for the long haul before the upcoming week by saying...it's for 400 bucks.  Should be pretty straight forward.  Got it done right around 1AM.

Not bad right?  You don't get paid too many side jobs at a rate of 200 an hour very often.  I don't hear much, but then get a call from the design company.  They say the client wants some small changes.  Then they say that I should talk directly to the client, could I meet at 9AM?  I say no to that, but agree to something after work.

I talk with the designer, he wants me to ok more work, and a red flag goes up.  Why on earth would he be wanting ME to ok work for him?  I'm not paying him...  Kind of weird but I move on.  Then I have to talk with the flash guy.  Then I finally talk with the client.  So far I've invested about 3 hours total now with all the talking involved.  I talk with the client and he's talking about javascript rollovers, dynamic ajaxy (his words) effects, and more graphics that he can change on the fly.

I make some more changes, and then get some more stuff off to this guy.  He's just not happy with anything.  He wants me to change the appearance of the page now, more javascript stuff, etc.  So far I'm 6 hours in, and not happy.  I tell him that he needs to be talking to the company that's giving me this job (when the hell did I become the PM for this thing).

He then goes to that company and essentially fires them.  I'm stressed out, wondering wtf just happened, and wondering if I'm going to see anything for all this trouble.  My Monday was pretty bad with me being so tired, and I'm still adjusting my sleep schedule 5 days later.  I finally get a hold of the project leader.

She says that she'd pay me the 200 bucks I was promised as I had done a lot of work and wasn't going to cheat me on that.  Wait...200 bucks, what happened to 400?  She also said that she didn't want to lose the client because they were a 100,000/year client.  And you're paying me 200 bucks out of that to do all this work?  I've just spent a Sunday working on this crap and now I'm now to $20/hour with all the time I've spent on this.  She then says that I will need to go through the friend of a friend to get the money.  Great, I'm now expecting $0/hour.

Lesson learned, when you feel just a tinge of "wtf is going on" just back away, turn around, and run like hell.

author: Willie | posted @ Friday, July 18, 2008 12:17 PM | Feedback (1)

Testing Homework


http://www.ayende.com/Blog/archive/2008/05/16/Rhino-Mocks--Arrange-Act-Assert-Syntax.aspx http://haacked.com/archive/2007/12/09/writing-unit-tests-for-controller-actions.aspx

author: Willie | posted @ Thursday, July 10, 2008 7:30 PM | Feedback (1)

A disadvantage with _memberVariable or m_variable


In the past I used to prefix my member variables with a _ to denote to myself that it was a member variable elsewhere in my code.  For more global variables I'd do a __ or double underscore.  Eventually, I gave up this practice as I'm always trying to sacrifice keystrokes without hampering readability and didn't really see the need anymore to keep it in there.  My IDE (VS2k5 or 2k8 now paired with Resharper) would bluntly tell me with it's Intellesense (or code completion) that the variable didn't exist within the scope, so I'd go figure out what the heck was up. Within my current codebase they are practicing the _memberName, and no real problems, but a distinct disadvantage in my mind cropped up over working in the past day.

It came when we were trying to set a member variable within the scope of the class to a certain value. 

_blah = true;

Straight forward enough, the value should have been true, and all was right in the world.  Problem is, on the UI side, the _blah = true didn't make anything happen.  The Blah button never changed it's state.  Upon inspection it turned out the _blah was being set within by a property:

public bool Blah{
   set {
      if (_blah!= value) {
         _blah = value;
         BlahChanged();
      }
   }

Code aside, the fact is, the setter was actually firing off an event to the UI to let it know that things had been updated.  Now if my _blah would have been blah instead, I would have seen the blah and Blah next to each other in my code completion list.  With the prefix underscore, the initial _ filtered out the Blah from my list of variables and it required inspection that interrupted the code though flow to see wtf was going on.  In my book, that's a negative thing.

author: Willie | posted @ Wednesday, July 09, 2008 9:06 AM | Feedback (0)

Problems with Attached Database in Vista


I believe the problem started because I had Visual Studio on the machine before I installed the Management Studio and it's software.  I initially uninstalled the original Sql Server Express then reinstalled everything.  Before, connecting to it from a web app or application I could get in fine, but now was running into "Generating user instances in SQL Server is disabled. Use sp_configure 'user instances enabled' to generate user instances."  Not cool.

What I initially did was to open up my master and msdb databases and run the command on each, along with a second "reconfigure" command after it.  Then I restarted the server's service.  A new error emerged: "Failed to generate user instance of SQL Server due to a failure in starting the process for the user instance.  The connection will be closed."

This thread helped a lot as I did eventually need to remove some files  but under Vista there is no C:\Documents and Settings\<your user account name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS.  In fact if you click on "Document and Settings" if you have the "show system files" selected then you will get an access denied message.  Turned out that these shortcuts are "junctures" and will give you this wierd error instead of redirecting you like you'd think they would.

I read through this article that went over just that and was able to then point to C:\Users\<User Name>\AppData\Local\Microsoft\Microsoft SQL Server Data instead of the XP equivalent of C:\Documents and Settings\<User Name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS.

After removing those files, and then trying to reconnect, the master, msdb, and others were recreated and all was well in the world again.

author: Willie | posted @ Monday, June 09, 2008 11:12 AM | Feedback (0)