Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Get Firefox

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

March 2007 - Posts

Things To Consider When Creating An Undo Mechanism

Brainstorming an undo-redo mechanism at work today and here are a couple of thoughts:

  1. There are so many mechanisms that already exist. A quick google on CodeProject gives five right away (without extra digging). NIH is foolish. :)
  2. Undo actions are transactions. However, they might not be just for your business objects, but also for your UI. An undo transaction might be only for the UI (but not likely).
  3. We've considered using Action/Reverse-Action, but this is double the code and development time. Reverse actions might also not be possible at times, since multithreading and events might cause non-deterministic action order.
  4. System.Transactions is nice, but it only works in one way - Undo. Creating a Redo with dual transaction directions might work, but it seems like a kludge.
  5. This article offers the closest solution to what we need, but it causes debugging pains and supports neither transactions nor UI element changes.

Anyone know of any good solutions?

A C# Riddle, Try #2

The code below compiles (sorry about that again) and is actually the code problem I originally wanted to post. Now play the compiler - which is Bar's base class and why:

namespace NS
{
    namespace NS
    {
        class Cls // Candidate #1
        {
        }
    }

    class Cls // Candidate #2
    {
    }

    namespace Foo
    {
        class Bar : NS.Cls // Who am I?
        {
        }
    }
}

For extra credit, how do I force the compiler to choose the other one?

[Update: Good going, everyone! Candidate #2 is indeed the one used. To use candidate #1, you must either apply the 'global::' prefix or explicitly use the class's full name.]

Posted: Mar 09 2007, 01:07 PM by Omer van Kloeten | with 7 comment(s)
Filed under:
MSDA '07 Videos Released

Yosi has announced that the presentations from the Microsoft Developers' Academy 2007 event are online.

Most of the presentation files I've seen are in English, but the videos are of the lecturers talking in Hebrew (except for Ron Jacobs's lecture), so if you're not a Hebrew speaker, you can skip downloading the videos.

Bubbling Windows Forms Events With Anonymous Methods

The following code illustrates a great use for anonymous methods.

public class MyContainerControl : Control
{
    private class MySpecializedControl : Control
    {
        protected virtual void OnMyEvent(EventArgs e)
        {
            if (this.MyEvent != null)
                this.MyEvent(this, e);
        }

        public event EventHandler MyEvent;
    }

    private MySpecializedControl special;

    public MyContainerControl()
    {
        this.special = new MySpecializedControl();
        // ...
        this.Controls.Add(this.special);

        this.special.MyEvent += delegate(object sender, EventArgs e) { this.OnMyEvent(e); };
    }

    protected virtual void OnMyEvent(EventArgs e)
    {
        if (this.MyEvent != null)
            this.MyEvent(this, e);
    }

    public event EventHandler MyEvent;
}
Generic Windows Forms Controls

Today I discovered that the Windows Forms Designer in Visual Studio 2005 doesn't know how to handle generics. For instance, the following example:

private class MyControl<T> : System.Windows.Forms.Control
{
}

could not be dropped in a Windows Forms designer (more on that in a little bit). If you go and decide to be a smart ass (like I usually am) and cheat the designer, you could drop a Panel (or any other control and manually edit the code in the InitializeComponent method and the Form.Designer.cs file to the class you want like so:

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.panel1 = new MyControl<int>();

    // ...
}

#endregion

private MyControl<int> panel1;

This would throw the designer into a hissy fit:

 

The solution? Here are my suggestions for the VS Team:

  1. Dropping a generic control - My suggestion is that when a generic control is dropped, each type parameter will have a default value (can be decided on by using attributes, much like the DefaultValueAttribute) and will be editable in the property grid with a specialized designer.
  2. Having a generic control on a Form - It's a bug and should be fixed. I know for certain that I am not the first person to come across this problem and that there is a workaround, but this is just annoying.
A C# Riddle

I asked this one during the last blogger dinner, so here it is for everyone else (and those who couldn't hear me there):

namespace A {
    namespace B {
        public class C {} // #1 - A.B.C
    }

    public class B {
        public class C {} // #2 - A.B+C
    }

    public class D : B.C {}
}

Without compiling the code above (which actually does compile), what is D's base class's full name (both choices are marked) and why?

[Update: Is my face red or what... The code above doesn't compile, although it's hard for me to understand why, since I could swear it did a month ago when I found out about this. I'll try and find what my fault was later today.]

[Update #2: Sorry about that. I've posted the correct riddle here.]

Posted: Mar 08 2007, 04:51 PM by Omer van Kloeten | with 11 comment(s)
Filed under:
New Job, New Challanges

This Sunday I started working for the Sela Group's Software Labs, as a consultant (among other titles).
My first job is at a small startup, where I act as reinforcements in the development of their Windows Forms user interface.

This means that I'm going to try and kickstart my weblog once more (for, like, the fifth time or something), given the fact that a dozen hours of development each day mean I'll be exposed to newer and cooler stuff more often.

More Posts