My Software Notes

Useful things I discover

Archive for March 2009

Updating a DataSource that is bound to a Windows Forms Control

with one comment

Do you ever get that annoying problem where you have bound your business object to some controls on a Windows Form and yet after you make a change to the data in a control, your change is not moved to the underlying data source?

I had that happen today, but only when I had made the change through code and then clicked on the Okay button immediately after.  The change I’d made just before clicking the button was not put into the business object.

I had, of course, put a BindingSource between the business object and the controls.  You should always use a BindingSource because it provides the plumbing you need to really make data binding work.  I had used the Visual Studio designer to hook the controls to the BindingSource.  Everything seemed right, but I wasn’t getting my updates.

The problem I was hitting was that the code generated to create the DataBinding between the control property (in this case Text) and the underlying business object property (in this case DestinationRoot) was defaulting to a DataSourceUpdateMode of OnValidation:

textBoxDestinationRoot.DataBindings.Add(
    new Binding("Text", this.bindingSourceOptions, "DestinationRoot", true,
        DataSourceUpdateMode.OnValidation));

What happened was that I made the change in the text box through code, clicked Okay, and the dialog closed. OnValidation didn’t fire because the control never had focus, so the change I made never go into my business object.  What I needed was an update mode of OnPropertyChanged. So I went into the generated code and change it to … just kidding.  (I have actually seen someone do that.)

I used the VS08 IDE.  Click on the control you want to fix the bindings on and open the handy-dandy Properties window. At the top (if you sort it alphabetically) there is a “(DataBindings)” property that you can expand. The most common properties to bind to are listed but there is also an “(Advanced)” property and that’s what you want to use. Click on the “…” button and the “Formatting and Advanced Binding” dialog opens. You’ll find that EVERY property of the control that can be bound to is listed there. The dialog lets you select the control’s Property, the DataSource to bind to, how you want to format the displayed value AND a combo called “Data Source Update Mode”, which is where you can change when your underlying data gets updated.

I made the change on my text box and the generated code looked like this (I took out the namespaces the designer loves to put in to make your lines of code 15 feet long):

textBoxDestinationRoot.DataBindings.Add(
    new Binding("Text", this.bindingSourceOptions, "DestinationRoot", true,
        DataSourceUpdateMode.OnPropertyChanged));

Now my dialog box works perfectly.

If you are having troubles in the opposite direction, that is you update your underlying data and your UI controls don’t reflect the change, then you need to implement the INotifyPropertyChanged Interface.

Written by gsdwriter

March 19, 2009 at 1:29 pm

Posted in Languages, Tools, UI

Tagged with , ,

Why do Wikipedia articles rank so high in Google?

with 2 comments

Is it because Wikipedia provides the most accurate data?  Or maybe because a huge number of links refer to the article?  Or is it some other logical explanation? 

The answer seems to be “None of the above.”  Read this article about the “SearchFest 09” conference and about 3/4 of the way down the page you’ll see this:

Dan Boberg quoted Google CEO Eric Schmidt as saying, “Wikipedia is mankind’s greatest gift to mankind.” I can’t find that quote online, so make no guarantee of authenticity; I am quoting Dan Boberg, not Eric Schmidt.

But if true, that is certainly revelatory as to WHY a lame Wikipedia article, written by people with an axe to grind or an apple to polish, rank so highly.

Because Eric likes Wikipedia.

I personally am not a fan of Wikipedia for the same reasons the writer of the article gives. I think it is way overrated. I find it helpful at times when I can’t get the data from elsewhere, but this slavish acceptance of Wikipedia as the ultimate “authority” on all human knowledge seems to me very foolish.

If you really want to know about something then find several sources, don’t just lazily click on the Wikipedia link.  It is important to know the source of your information but on Wikipedia you don’t know who wrote the article and you don’t know if they have “an axe to grind or an apple to polish.”  So think twice before blindly accepting Wikipedia articles as gospel truth.

Written by gsdwriter

March 15, 2009 at 11:05 am

Posted in Rant

Tagged with

Superstition and Software

leave a comment »

I’m not superstitious (touch wood) so I’m not afraid to release version 4.1.13 of some software today, Friday the 13th.

One of the theories as to why this day is considered unlucky involves the demise of the Knights Templar, who fell foul of the greed and jealousy of King Phillipe the Fair of France.  The King had the French Templars arrested on Friday the 13th of October 1307 and subsequently stole their lands and treasure.  With the smart usage of medieval PR techniques and a great deal of violence, the King managed to totally destroy the  Templars by 1314.

Well, I’m not a Knights Templar, so why should I be worried?

Just as an aside, I don’t know if you knew this, but Office 2007 was version 12, however Office 2010 is not going to be version 13, it will be version 14.  What happened to version 13, you ask?  Well, it got skipped.  So even in this age of technology, science and reason, the world’s leading software company is still scared of the evil number 13.

Anyway, you can read more about this interesting day here:  Friday the 13th

So, as I said, I’m not scared that … oh my god!  My build machine just exploded!

Written by gsdwriter

March 13, 2009 at 11:17 am

Posted in Humor

Tagged with ,

C# 3.0 Properties

leave a comment »

Don’t you just love the new Property syntax in C# 3.0?  I got so tired of having to create a private class level variable and then click on the “Refactor”, encapsulate the variable, then cut and paste the generated code to the place in my code where I wanted it.

Now I just type:

public string MyProperty { get; set; }

and I’m done!

In case you didn’t know you can also specify the access modifier for the “get” or” set”:

public string MyProperty { get; private set; }

That way you can treat the “set” like a private class level variable and to the outside world the property looks read only. Or make it “protected” and only child classes can “set” it.

This works because the “get” and “set” are implemented as separate methods in the IL that is generated when you build your app.

One other tip.  If you have created an Interface that contains a read-only property and you don’t want to have to implement a private class level variable to hold the value then you can add a private or protected “set” to the property.  Here’s an example:

public interface IMyContract
{
    string MyProperty {get; }
    //other stuff ...
}

public class MyClass : IMyContract
{
    public string MyProperty { get; private set; }
    //other stuff ...
}

This works for the same reason I gave above, i.e., the “get” and “set” are implemented as different methods in the IL so you have implemented the Interface with the “get” and then adding your own “set” doesn’t violate the Interface contract because it is a separate method.

Written by gsdwriter

March 4, 2009 at 6:46 pm

Posted in Languages

Tagged with , ,

“var” is a pronoun

with one comment

One of the great features of C# 3.0 is the new “var” keyword.  It allows the type of your local variables to be inferred and thus saves you from too much typing (no pun intended).

For example, instead of having to type:

MyClassWithAnIncredblyLongName foo = new MyClassWithAnIncredblyLongName();

You can type:

var foo = new MyClassWithAnIncredblyLongName();

However, this convenience can be taken too far.  When I first started using C# 3.0 I got all enthusiastic about using var, but it didn’t take me long to recognize the pitfalls.  It’s fine to use “var” as in the example above, but what about this:

var guyToHandle = GetTheGuy(someVariable);

So what type is guyToHandle? Sure you can move the mouse over “var” in Visual Studio and the tool tip will tell you, but wouldn’t it be easier to read and therefore maintain if it said:

Employee guyToHandle = GetTheGuy(someVariable);

Now with just a quick look you can see what the type is. No mouse required.

The equivalent in the English language to “var”is a pronoun and similar problems can occur when a pronoun is misused:

Joe and Bill were talking and he said “blah, blah, blah”.  Which “he” is it?  Hence the title of this post.

As with any language feature, “var” is a tool to use for the appropriate job.  So use it wisely.

Written by gsdwriter

March 4, 2009 at 11:57 am

Posted in Languages

Tagged with ,

The problem with Dotfuscator CE

with one comment

Dotfuscator CE works okay for simple code but add something more complex, like for example your own generic classes, and it fails.

I tried Dotfuscator CE on a solution consisting of six assemblies.  One of the main assemblies used a generic base class for the business objects.  Dotfuscator ran with no errors reported, but when I tried to run the code I immediately got exceptions.

I traced them down to my custom generic classes.  Because the CE version of Dotfuscator allows you no fine tuning then you are left with an “all or nothing” proposition.  If the offending assembly won’t run when obfuscated then your only choice is to not obfuscate it.  This was not acceptable for my project, so I had to find an alternative.

I settled on Phoenix Protector, a freeware application that lets you fine tune your obfuscation and has a command line that actually works.  It’s not going to be my long-term solution, but it is sufficient for the moment.

Written by gsdwriter

March 3, 2009 at 8:45 pm

Posted in Tools

Tagged with , ,

Using Dotfuscator Community Edition

leave a comment »

Just in case you didn’t know, Dotfuscator is an obfuscator that comes with Visual Studio. The version in VS is the “Community Edition,” an emasculated version of the “Standard” and “Professional” products.

My first thought was that PreEmptive Solutions the makers of Dotfuscator would have made the community edition really easy to use and would have provided fast step-by-step instructions to tell you how to integrate it into your build. But no, it was not to be. Although the GUI is easy to use, the command line version (in the Community Edition) is not really a command line version.

The first thing you need to know is that the command line doesn’t really work in the CE version. All it does is open the GUI with the parameters you’ve passed in (better than nothing I guess) and then you have to click the “build” button. Also it doesn’t run unless the VS UI is open. Do you get the idea they REALLY want you to buy either Standard or Professional? I don’t blame them for that, but it would help if they gave you documentation specifically for the CE edition, and not just for Pro, leaving it to you to figure out how much of it doesn’t apply.

Anyway, basic steps:
1. You create a Dotfuscator “project” for your application. Just follow the standard instructions for creating a project in the GUI. The project file is really just an XML file that contains the data needed by the program to obfuscate your assemblies.
2. Now create a Visual Studio “Post-Build” event in the “Properties” page of your assembly that calls dotfuscator and creates your obfuscated assembly. (Command Line Options) Here is an example command line (I split it across four lines so it wouldn’t make this post wider than your monitor):

"C:\Program Files\Microsoft Visual Studio 9.0\
Application\PreEmptive Solutions\
Dotfuscator Community Edition\dotfuscator" -v
"$(ProjectDir)$(OutDir)TestObfuscDF.xml"

Don’t forget the quotes around the paths and that includes $(ProjectDir), etc. which are macros supplied by the Post-Build Dialog.

3. When you run your build from the VS UI, the Dotfuscator UI will open when your post-build event is hit and you’ll have to click on the “Build” button. The VS build execution pauses until you close the Dotfuscator UI, then it will finish.

So there you have it.

Here is a list of other .NET obfuscators: .NET Obfuscation.

I also came across an open-source .NET obfuscator but you can only get at the source-code if you buy the eBook: Desaware: Obfuscating .NET: Protecting your code from prying eyes.

Written by gsdwriter

March 3, 2009 at 3:26 pm

Posted in Tools

Tagged with , ,

What’s this?

leave a comment »

This is a blog where I will be making notes of useful things I come across.  From the title of the blog you can surmise that the “useful things” will be software related.

Written by gsdwriter

March 3, 2009 at 3:17 pm

Posted in Uncategorized