|
-
Nov 9th, 2002, 05:12 PM
#1
Thread Starter
yay gay
events & delegates
hmmm i've been reading about this and i got a few questions about it...
hmm...what is the advantage of my class inherit from EventArgs? if i do so i can't make my class inherit from anymore class so it looks like a disavantage
and...i want to have events that has different parameters between them...do i have to have 1delegate for each "type" of parameters? maybe the answer is inheriting from EventArgs..omg..i am not getting this lol
-
Nov 9th, 2002, 05:49 PM
#2
Hyperactive Member
It depends what you are trying to do...
If you are trying to supply an event procedure, like from a user control, for any custom event you want, you will need to create a class inherited from EventArgs. Actually, the way I understand this from MS is that if you want others to be consuming these events like for a class library or user control, then you should pack any custom arguments into a EventArgs subclass.
Are delegates CLS-Compliant? I'm not sure VB has delegates...
If however you are looking for something more like a C callback function, something that you are using internally, you should use just a delegate. The delegate object will be a bit more direct than stuffing all the arguments into your EventArgs subclass.
hmmm....after reading your post again, I'm not sure I answered your question...
Only arguments for custom event procedures need to be a subclass of EventArgs. If you have some simple events, like a simple notification, you can just use the standard EventArgs class.
PHP Code:
// simple notification:
public delegate void ChangedEventHandler(object sender, EventArgs e);
If you want to add some other parameters to the event, then create a class, CustomEventArgs inherited from EventArgs that have some additional public properties.
PHP Code:
// custom notification:
public delegate void CustomEvent(object sender, CustomEventArgs e);
...let me know if that's still confusing and I will try again...
-scott
he he he
-
Nov 9th, 2002, 05:50 PM
#3
Frenzied Member
Dont gain the world and lose your soul
-
Nov 9th, 2002, 06:00 PM
#4
Thread Starter
yay gay
i want to make a class that has events...but how do i make that several events use the same delegate if they have different parameters?
-
Nov 9th, 2002, 06:20 PM
#5
Frenzied Member
You cant. The method you are calling have to have the same signiture of the delegate, so each event that you want to have a diferent parameter, must match the delegate that will invoke it.
eg:
Code:
public delegate int CompareNumsDelegate(int num1, int num2);
//method
int MyFunc(int num1, int num2)
{
if(num1 > num2)
{
return num1;
}
else
return 0;
}
CompareNumsDelegate comDel = new CompareNumsDelegate(MyFunc);
You can now call comDel just as if it was the function MyFunc like this:
Last edited by DevGrp; Nov 10th, 2002 at 06:46 PM.
Dont gain the world and lose your soul
-
Nov 9th, 2002, 06:58 PM
#6
Thread Starter
yay gay
so thats why ppl put things under EventArgs?
if yes how do i do that?
-
Nov 9th, 2002, 07:03 PM
#7
Frenzied Member
so thats why ppl put things under EventArgs?
What do you mean?
Dont gain the world and lose your soul
-
Nov 9th, 2002, 07:49 PM
#8
Thread Starter
yay gay
i have a class for internet i want an event for Connected other for Disconnected blablabla they all have different parameters...will i have to make 300 delegates + 300 events? omg there must be another way
-
Nov 9th, 2002, 09:39 PM
#9
Frenzied Member
I'm sure you wont need all 300 . What I would do is make classes inheriting from EventArgs, then extending those classed to include the functionality for events that you would like.
eg.
Class DisconnectedEventHandler
Class ConnectedEventHandler
Then group the functionality.
Dont gain the world and lose your soul
-
Nov 10th, 2002, 09:15 AM
#10
Thread Starter
yay gay
im not getting
-
Nov 10th, 2002, 11:12 AM
#11
Frenzied Member
What are the events that you need?
Dont gain the world and lose your soul
-
Nov 10th, 2002, 11:20 AM
#12
Thread Starter
yay gay
lets imagine i want this events:
connected(string ip)
disconnected(string exitMessage, string ip)
receivedMessage(string message, string ip, string nickName)
etc..
will i have to make a delegate for each one of these because they all have differente params?
-
Nov 10th, 2002, 11:57 AM
#13
Frenzied Member
Dont gain the world and lose your soul
-
Nov 10th, 2002, 11:57 AM
#14
Thread Starter
yay gay
-
Nov 10th, 2002, 12:02 PM
#15
Thread Starter
yay gay
ah what names do u give to the delegates?
maybe givin them names like param1 meaning this has only 1 param would help or no? lol to me this thing of delegates is the worse **** ever...in vb.net we have delegates but dont have to use them in events what seems a LOT better
-
Nov 10th, 2002, 12:33 PM
#16
Thread Starter
yay gay
hmm the usual when programming in C# is somewhat for each event a delegate?
-
Nov 10th, 2002, 12:41 PM
#17
Thread Starter
yay gay
how do i make to put delegates and events in the same class?
the book only says how to put the delegates under the same namespace...
upper:
PHP Code:
namespace WindowsApplication5
{
public delegate void Event(string message);
class
PHP Code:
class TESTE
{
public event Event teste;
private void lol()
{
teste("...");
}
}
form
PHP Code:
private void Form1_Load(object sender, System.EventArgs e)
{
TESTE test = new TESTE();
test.teste += new Event(my_teste);
my_teste("lol...");
}
private void my_teste(string mensagem)
{
MessageBox.Show(mensagem + "\nlol sukas");
}
}
-
Nov 10th, 2002, 12:45 PM
#18
Frenzied Member
Ok, I have a little example;
PHP Code:
//Class with your events and delegates
public class Tester
{
public delegate void ConnectedHandler(string ip);
public delegate void DisconnectedHandler(string exitMsg, string ip);
public delegate void ReceivedMessageHandler(string msg, string ip, string nick);
public event ConnectedHandler Connected;
public event DisconnectedHandler Disconnected;
public event ReceivedMessageHandler ReceiveMessage;
public Tester()
{
}
public void Connect(string ip)
{
if(Connected != null)
{
Connected("Connected to " + ip);
}
}
public void Disconnect(string ip)
{
if(Disconnected != null)
{
Disconnected("Disconnected from ", "191.168.0.1");
}
}
public void Receive()
{
if(ReceiveMessage != null)
{
ReceiveMessage("How are you", "191.168.0.1", "Dev");
}
}
}
Using the Events
PHP Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestEvents
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.FolderBrowserDialog fd;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
Tester t = new Tester();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
Application.EnableVisualStyles();
t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);
t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);
}
private void Form1_Load(object sender, System.EventArgs e)
{
t.Connect("191.168.0.1");
t.Disconnect("191.168.0.1");
t.Receive();
}
private void t_Connected(string ip)
{
MessageBox.Show(ip);
}
private void t_Disconnected(string exitMsg, string ip)
{
MessageBox.Show(exitMsg + ip);
}
private void t_ReceiveMessage(string msg, string ip, string nick)
{
MessageBox.Show(msg + " " + nick + " from " + ip);
}
}
}
Dont gain the world and lose your soul
-
Nov 10th, 2002, 01:25 PM
#19
Thread Starter
yay gay
ah ill have to put this always in the form?
t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);
t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);
bleh..thats g@y
-
Nov 10th, 2002, 01:51 PM
#20
Thread Starter
yay gay
imagine i have a class that i do..then i send that class to my friend..in order to put that class + class' events working he'll have to put that
t.ReceiveMessage +=new TestEvents.Tester.ReceivedMessageHandler(t_ReceiveMessage);
t.Connected +=new TestEvents.Tester.ConnectedHandler(t_Connected);
t.Disconnected +=new TestEvents.Tester.DisconnectedHandler(t_Disconnected);
things in his form? isnt there an hit and run like vb way of doin this? it seems so complicated damn...waste of time in comp. with vb
-
Nov 10th, 2002, 02:05 PM
#21
PowerPoster
It seems like a waste of time, but the advantages outweight the inconvienence. You can add more than one procedure to be called to the delegate if you want. This means that if you have multiple forms that must recieve the event, they can all subscribe to the event. Another way they are useful is you can turn off and on the event at run time.
Now, if you create a control with events, the user of your control should be able to let the IDE subscribe to the event for them by clicking the Event button in the properties window and double clicking the event.
What they provide is more flexibility.
Sure VB hides it from you, but can you really call yourself a programmer without knowing how they really work?...lol.
-
Nov 10th, 2002, 02:47 PM
#22
Thread Starter
yay gay
it just seems damn confusing
-
Nov 10th, 2002, 02:52 PM
#23
Thread Starter
yay gay
ah i think im getting in now after watching carefully DevGroup's example..but it's still confusing lolol
-
Nov 10th, 2002, 02:57 PM
#24
Thread Starter
yay gay
woohooo tested in a class now made by me and worked lol i rule
-
Nov 10th, 2002, 06:42 PM
#25
Frenzied Member
Good to know that it works and you're finally getting it. Dont worry about the confusion, it will pass . Sometimes it still a little confusing for me, then I have to pull out my notes .
Dont gain the world and lose your soul
-
Nov 11th, 2002, 07:20 AM
#26
Thread Starter
yay gay
now i saw a class i downloaded from net and only now i realised that for each event i have to make a delegate..i though there was a way to have only 1delegate for all events lol
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|