tags:

views:

109

answers:

1

I have an interface for a UI widget, two of which are attributes of a presenter.

public IMatrixWidget NonProjectActivityMatrix {
        set {
            // validate the incoming value and set the field
            _nonProjectActivityMatrix = value;
            ....
            // configure & load non-project activities
     }

public IMatrixWidget ProjectActivityMatrix {
        set {
            // validate the incoming value and set the field
            _projectActivityMatrix = value;
            ....
            // configure & load project activities
   }

The widget has an event that both presenter objects subscribe to, and so there is an event handler in the presenter like so:

public void OnActivityEntry(object sender, EntryChangedEventArgs e) {
    // calculate newTotal here
    ....
        if (ReferenceEquals(sender, _nonProjectActivityMatrix)) {
            _nonProjectActivityMatrix.UpdateTotalHours(feedback.ActivityTotal);
        }

        else if (ReferenceEquals(sender, _projectActivityMatrix)) {
            _projectActivityMatrix.UpdateTotalHours(feedback.ActivityTotal);
        }
        else {
            // ERROR - we should never be here
        }
    }

The problem is that the ReferenceEquals on the sender fails, even though it is the implemented widget that is the sender - the same implemented widget that was set to the presenter attribute!

Can anyone spot what the problem / fix is?

Cheers, Berryl

I didn't know you could edit nicely. Cool. Here is the event raising code:

void OnGridViewNumericUpDownEditingControl_ValueChanged(object sender, EventArgs e)
    {
 // omitted to save sapce

        if (EntryChanged == null) return;

        var args = new EntryChangedEventArgs(activityID, dayID, Convert.ToDouble(amount));
        EntryChanged(this, args);
    }

Here is the debugger dump of the presenter attribute, sans namespace info:

?_nonProjectActivityMatrix
{WinPresentation.Widgets.MatrixWidgetDgv}
[WinPresentation.Widgets.MatrixWidgetDgv]: {WinPresentation.Widgets.MatrixWidgetDgv}

Here is the debugger dump of the sender:

?sender
{WinPresentation.Widgets.MatrixWidgetDgv}
base {Core.GUI.Widgets.Lookup.MatrixWidgetBase<Core.GUI.Widgets.Lookup.DynamicDisplayDto>}: {WinPresentation.Widgets.MatrixWidgetDgv}
_configuration: {Domain.Presentation.Timesheet.Matrix.WeeklyMatrixConfiguration}
_wrappedWidget: {Win.Widgets.DataGridViewDynamicLookupWidget}
AllowUserToAddRows: true
ColumnCount: 11
Count: 4
EntryChanged: {Method = {Void OnActivityEntry(System.Object, Smack.ConstructionAdmin.Domain.Presentation.Timesheet.Matrix.EntryChangedEventArgs)}}
SelectedCell: {DataGridViewNumericUpDownCell { ColumnIndex=3, RowIndex=3 }}
SelectedCellValue: "0.00"
SelectedColumn: {DataGridViewNumericUpDownColumn { Name=MONDAY, Index=3 }}
SelectedItem: {'AdministrativeActivity: 130-04', , AdministrativeTime, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}
  • Berryl
A: 

Off the top of my head I can think of these reasons why ReferenceEquals will return false:

  • sender is null - Verify that the widget sends this as the sender parameter when raising the event.
  • The widget is not assigned to the presenter for some reason, so it is null
  • sender is not what you think it is (how have you verified that it is indeed the sender you expect)
  • The widget is implemented as a struct, not a class

There may be more, but this is where I would start (in that order).

Update: one thing that catches my eye is this:

_wrappedWidget: {Win.Widgets.DataGridViewDynamicLookupWidget}

Could it be that sender is an object that wraps your widget? If that is the case, ReferenceEquals will naturally return false when doing the comparison.

Fredrik Mörk
The widget is a wrapper for a DataGridView (a class). I'm checking the debugger dumps (and how I know it isn't null) to the remaining two possibilities on your list:Here is the presenter attribute dump, complete with namespace info:?_nonProjectActivityMatrix{Smack.ConstructionAdmin.WinPresentation.Widgets.MatrixWidgetDgv} Hi Fredrik[Smack.ConstructionAdmin.WinPresentation.Widgets.MatrixWidgetDgv]: {Smack.ConstructionAdmin.WinPresentation.Widgets.MatrixWidgetDgv}I don't quite get the part that looks like an index (widget[widget])
Berryl
{Smack.ConstructionAdmin.WinPresentation.Widgets.MatrixWidgetDgv} base {Smack.Core.GUI.Widgets.Lookup.MatrixWidgetBase<Smack.Core.GUI.Widgets.Lookup.DynamicDisplayDto>}: {Smack.ConstructionAdmin.WinPresentation.Widgets.MatrixWidgetDgv} _configuration: {Smack.ConstructionAdmin.Domain.Presentation.Timesheet.Matrix.WeeklyMatrixConfiguration} _wrappedWidget: {Smack.Win.Widgets.DataGridViewDynamicLookupWidget}I left the data grid view debugger info off.
Berryl
That didn't format very well. The 2nd comment is the type info from the debugger dump of the widget.
Berryl
The event raising code: void OnGridViewNumericUpDownEditingControl_ValueChanged(object sender, EventArgs e) { // omitted to save sapce if (EntryChanged == null) return; var args = new EntryChangedEventArgs(activityID, dayID, Convert.ToDouble(amount)); EntryChanged(this, args); }
Berryl
Add code samples to your original question insead; it's easier to format the code there, and it also makes the code samples easier to locate for other users that may be able to provide answers :)
Fredrik Mörk
No Fredrik, although the terminology in my code isn't helping here. The _wrappedWidget in the code is the DataGridView itself, and is held by the MatrixWidgetDgv. The _wrappedWidget is the original sender but my event raiser in the Matrix is passing itself as the sender. See my comments to Henk above. Thx
Berryl