event handling and delegete

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!

My problem is that I have several methods that is quite similar. Each one
works as they should.
You see two of them just below.
I have removed several statements that is not relevant for my question.
My idea is to have only one method and pass argument for things that differ
between the methods.
As you can see there isn't much that differ. It's two rows.
I don't know how to pass the things that differ. I have prefix those rows
that differ with the word "Diff"

Can you give example how I should write?

private void addNewBlowStep( ArrayList list, FlyGrid flygrid )
{
StringClass cond = new StringClass("", new ArrayList());

StringClass sc = new StringClass( "", list, cond );

Diff sc.BlowStepEvent += new MyDelegateSignature(HandleBlowStepEvent);

Diff cond.BlowStepCondEvent += new
MyCondDelegateSignature(HandleBlowStepCondEvent);

m_flygrid.AddRow( new object[] { sc, cond} );
}

private void addNewAdditionStep(ArrayList list, FlyGrid flygrid )
{
StringClass cond = new StringClass("",new ArrayList());

StringClass sc = new StringClass( "", list, cond );

Diff sc.AdditionEvent += new MyDelegateSignature(HandleAdditionEvent);

Diff cond.AdditionCondEvent += new
MyCondDelegateSignature(HandleAdditionCondEvent);

m_flygrid.AddRow( new object[] { sc, cond} );
}



//Tony
 
Hi,

Frankly it's not very clear what you want to achieve.

Can you use an extra parameter to indicate which event you want to handle?
you could create an enumerator for this situation
 
Hello!

I want to pass an argument that match both BlowStepEvent and
AdditionEvent.
I also want to have an argument that match both BlowStepCondEvent and
AdditionCondEvent.
I also want to have an argument that match both HandleBlowStepEvent and
HandleAdditionEvent.
Finally I want to have an argument that match both HandleBlowStepCondEvent
andHandleAdditionCondEvent.
It's like having an int type as formal parameter and as actual parameter I
can pass any whole number.
Assume we replace method addNewBlowStep and addNewAdditionStep with this
method called addNewStep below. So I wonder how should the type Sometype_1
and Sometype_2 and Sometype_3 and Sometype_4 look like. That is what I want.
Hope you understand what I mean.

addNewStep(ArrayList list, FlyGrid flygrid, Sometype_1 newArg_1,
Sometype_2 newArg_2, Sometype_3 newArg_3,
Sometype_4 newArg_4)
{
StringClass cond = new StringClass("", new ArrayList());

StringClass sc = new StringClass( "", list, cond );

sc.newArg_1 += new MyDelegateSignature( newArg_3);

cond.newArg_2 += new MyCondDelegateSignatur( newArg_4);

m_flygrid.AddRow( new object[] { sc, cond} );

}

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Frankly it's not very clear what you want to achieve.

Can you use an extra parameter to indicate which event you want to handle?
you could create an enumerator for this situation


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



tony said:
Hello!

My problem is that I have several methods that is quite similar. Each one
works as they should.
You see two of them just below.
I have removed several statements that is not relevant for my question.
My idea is to have only one method and pass argument for things that
differ
between the methods.
As you can see there isn't much that differ. It's two rows.
I don't know how to pass the things that differ. I have prefix those rows
that differ with the word "Diff"

Can you give example how I should write?

private void addNewBlowStep( ArrayList list, FlyGrid flygrid )
{
StringClass cond = new StringClass("", new ArrayList());

StringClass sc = new StringClass( "", list, cond );

Diff sc.BlowStepEvent += new MyDelegateSignature(HandleBlowStepEvent);

Diff cond.BlowStepCondEvent += new
MyCondDelegateSignature(HandleBlowStepCondEvent);

m_flygrid.AddRow( new object[] { sc, cond} );
}

private void addNewAdditionStep(ArrayList list, FlyGrid flygrid )
{
StringClass cond = new StringClass("",new ArrayList());

StringClass sc = new StringClass( "", list, cond );

Diff sc.AdditionEvent += new MyDelegateSignature(HandleAdditionEvent);

Diff cond.AdditionCondEvent += new
MyCondDelegateSignature(HandleAdditionCondEvent);

m_flygrid.AddRow( new object[] { sc, cond} );
}



//Tony
 
Tony,

The "norm" for handling events is to create a subclasses EventArgs class.

public delegate void MessageArrivalHandler (object sender , MyEventArgs
margs );

You code up MyEventArgs to hold whatever info you want.
If you want it to have a string property, then fine.
If you need an array list, fine.

Here is an example. I have 3 string properties. .
public class MyEventArgs : EventArgs

{

private string _messageBody;

private string _label;

private string _id;

public string Id

{

get { return _id; }

}

public string Label

{

get { return _label; }

}


public string MessageBody

{

get { return _messageBody; }

}

public MessageEventArgs(string id , string label , object body)

{

_id = id;

_label = label;

_messageBody = body;

}

}

In your case, maybe you want an
ArrayList
FlyGrid property,.... comiing off of the MyEventArgs class.




Then code up a method .. to match the delegate signature:

protected void MyMessageReceivedEventHandler(object sender , MyEventArgs
margs)

{



try

{



//do something with margs ..




}

finally

{



}

}




Then you code up the raised event.. to react to the event.

myobject.RaisedEvent += new
MessageReceivedEventHandler(MyMessageReceivedEventHandler);


That's what I'd suggest.
Its keeps the MyEventArgs very clean.... without alot of if / else this that
stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top