Handling EntityCollection Events

Posts   
 
    
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 03-Jan-2007 14:37:31   

Hi All,

Allow me first to wish you all a very happy new year!

Now for my problem. I'm handling the EntityAdding event like so;

AddHandler m_Current.EntityAdding, AddressOf entity_EntityAdding

Delegate;

Private Sub entity_EntityAdding(Of CancelableCollectionChangedEventArgs)(ByVal sender As Object, ByVal e As CancelableCollectionChangedEventArgs)

I have 2 questions here;

  • Why do I have to write the delegate this way? (Of CancelableCollectionChangedEventArgs)
  • I want to cancel, but [e] does NOT have a 'Cancel' property. What am I doing wrong? Thanks, Bashar
Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 03-Jan-2007 14:56:24   

Before an event handler becomes usable, you must first associate it with an event by using either the Handles or AddHandler statement.

Check this: http://msdn2.microsoft.com/en-us/library/k2kt7a7y(VS.80).aspx

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 03-Jan-2007 15:07:58   

Walaa wrote:

Before an event handler becomes usable, you must first associate it with an event by using either the Handles or AddHandler statement.

Check this: http://msdn2.microsoft.com/en-us/library/k2kt7a7y(VS.80).aspx

I've already done that, as explained in my previous post.

omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 03-Jan-2007 15:52:32   

The point that Bashar is trying to raise is that the Delegate that it is very artificial the way the delegate is written (using the generics operator (Of )

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 03-Jan-2007 16:44:41   

Can't you add the handler by adding "Handles event" or something? (I never understood this in VB.NET, so I might be wrong here).

Microsoft recommends to use EventHandler<T> instead of using derived eventhandler delegates just to pass different argument objects. So as a good boy I obeyed their recommendation. simple_smile

I don't find the method that awkward though, it's a generic handler, though vb.net should emit the stub for the method automatically, doesn't it do that? (like C#'s editor)

Frans Bouma | Lead developer LLBLGen Pro
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 03-Jan-2007 21:38:07   

Otis wrote:

Can't you add the handler by adding "Handles event" or something? (I never understood this in VB.NET, so I might be wrong here).

Nope, not when I need to handle a child entity collection, such as SaleDetail for example.

Otis wrote:

Microsoft recommends to use EventHandler<T> instead of using derived eventhandler delegates just to pass different argument objects. So as a good boy I obeyed their recommendation. simple_smile

I don't find the method that awkward though, it's a generic handler, though vb.net should emit the stub for the method automatically, doesn't it do that? (like C#'s editor)

No it doesn't confused and I'm unable to cancel the event now. Solution?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 03-Jan-2007 23:15:56   

Well, the property is there, so I don't know what's causing the problem. If you just set the property, do you get a compile error? It might be an intellisense glitch.

Frans Bouma | Lead developer LLBLGen Pro
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 04-Jan-2007 07:27:12   

Otis wrote:

Well, the property is there, so I don't know what's causing the problem. If you just set the property, do you get a compile error? It might be an intellisense glitch.

No. When I set the property like so:

e.Cancel = True

I receive the following error: 'Cancel' is not a member of 'CancelableCollectionChangedEventArgs'

Help confused

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 04-Jan-2007 07:32:03   

I should write the delegate like so:

Private Sub entity_EntityAdding(Of EventArgs)(ByVal sender As Object, ByVal e As CancelableCollectionChangedEventArgs)

This seemed to solve the problem of (e.Cancel) but another error popped up: 'Type arguments for method 'Private Sub entityDetail_EntityAdding(Of EventArgs)(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)' could not be inferred from the delegate 'Delegate Sub EventHandler(Of TEventArgs As System.EventArgs)(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)'

Are you sure there isn't something wrong here?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 04-Jan-2007 09:40:32   

Bashar wrote:

I should write the delegate like so:

Private Sub entity_EntityAdding(Of EventArgs)(ByVal sender As Object, ByVal e As CancelableCollectionChangedEventArgs)

No that's not correct, as that suggests EventHandler<EventArgs> is used (or better: EventHandler) in the class which raises the event, which isn't the case.

This seemed to solve the problem of (e.Cancel) but another error popped up: 'Type arguments for method 'Private Sub entityDetail_EntityAdding(Of EventArgs)(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)' could not be inferred from the delegate 'Delegate Sub EventHandler(Of TEventArgs As System.EventArgs)(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)'

Are you sure there isn't something wrong here?

this is the class:


/// <summary>
/// Event args which are used for collection changed events and which is raised by cancelable actions. Set the Cancel flag to cancel the action
/// which raised the event. 
/// </summary>
public class CancelableCollectionChangedEventArgs : CollectionChangedEventArgs
{
    #region Class Member Declarations
    private bool _cancel;
    #endregion

    /// <summary>
    /// Initializes a new instance of the <see cref="CancelableCollectionChangedEventArgs"/> class.
    /// </summary>
    /// <param name="involvedEntity">The involved entity.</param>
    public CancelableCollectionChangedEventArgs( IEntityCore involvedEntity )
        : base( involvedEntity )
    {
    }

    /// <summary>
    /// Gets / sets the cancel flag to cancel the action.
    /// </summary>
    public bool Cancel
    {
        get
        {
            return _cancel;
        }
        set
        {
            _cancel = value;
        }
    }
}

/// <summary>
/// EventArgs class for events raised by the entity collection classes for various actions. 
/// </summary>
public class CollectionChangedEventArgs : EventArgs
{
    #region Class Member Declarations
    private IEntityCore _involvedEntity;
    #endregion

    /// <summary>
    /// Initializes a new instance of the <see cref="CollectionChangedEventArgs"/> class.
    /// </summary>
    /// <param name="involvedEntity">The involved entity.</param>
    public CollectionChangedEventArgs( IEntityCore involvedEntity )
    {
        _involvedEntity = involvedEntity;
    }

    /// <summary>
    /// Gets / sets the entity involved in the action which took place and which triggered the raised event.
    /// </summary>
    public IEntityCore InvolvedEntity
    {
        get
        {
            return _involvedEntity;
        }
        set
        {
            _involvedEntity = value;
        }
    }
}

Cancel is there. (v2.0, .NET 2.0 code base)

The original method declaration you had in the first post is correct, what I find strange is that the Cancel property isn't accessable.

you could try as well:


Private Sub entity_EntityAdding(ByVal sender As Object, ByVal e As CancelableCollectionChangedEventArgs)

as the generic type specification is implicit anyway

Frans Bouma | Lead developer LLBLGen Pro
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 04-Jan-2007 11:58:24   

Otis wrote:

you could try as well:


Private Sub entity_EntityAdding(ByVal sender As Object, ByVal e As CancelableCollectionChangedEventArgs)

as the generic type specification is implicit anyway

I'm afraid not. I get the following error when I try that: 'Method 'Private Sub entityDetail_EntityAdding(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(Of TEventArgs As System.EventArgs)(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)'

Anything else I can do?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 04-Jan-2007 12:41:09   

Not that I know of. disappointed Though the usage of EventHandler(Of EventArgs) is very common, isn't there a google groups thread available on this?

If you use the original method declaration, and you ignore intellisense, you'll get the compile error that 'Cancel' isn't a property, correct? Which properties do you see in intellisense on 'e' ?

Frans Bouma | Lead developer LLBLGen Pro
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 04-Jan-2007 13:09:15   

Correct.

Properties are:

  • Equals
  • GetHashCode
  • GetType
  • ReferenceEquals
  • ToString There might be one thing though, I believe Omar's done something here that might've screwed things up.

I'll ask him when he arrives at Las Vegas (he's there for some Vista training).

I'll keep you updated.

Thank you very much for your efforts Frans! simple_smile

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 06-Jan-2007 12:41:10   

I still did NOT get in touch with Omar, however, there are new updates to the problem.

When I try to handle the EntityAdded event using the Handles operator I'm stunned with the following error:

'Private Sub ecBill_EntityAdding(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)' cannot handle Event 'Public Event EntityAdding(sender As Object, e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs)' because they do not have the same signature.'

confused

What is wrong here? simple_smile

Private Sub ecBill_EntityAdding(ByVal sender As Object, ByVal e As SD.LLBLGen.Pro.ORMSupportClasses.CancelableCollectionChangedEventArgs) Handles ecBill.EntityAdding
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 07-Jan-2007 11:28:35   

Huh confused that's indeed VERY strange.

Do you by any chance have Linq CTP's installed on your system?

Could you check if the code gives the same error on another machine?

Frans Bouma | Lead developer LLBLGen Pro
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 07-Jan-2007 11:46:13   

Otis wrote:

Huh confused that's indeed VERY strange.

I know! disappointed

Otis wrote:

Do you by any chance have Linq CTP's installed on your system?

Nope.

Otis wrote:

Could you check if the code gives the same error on another machine?

I don't have to, I created another non-related project and guess what: it works just fine frowning

I have NO idea what is going on. I thought there is some kind of version problem, but, nope, there is nothing wrong with the versions, I think.

Any other ideas?

[Update]flushed It was a version problem. It seems I was using an old version of the DQE in my DAL DB Specific. I'm sorry for all the trouble you had to go through.