Click to See Complete Forum and Search --> : Cutoms classes and events - I can't get the events to work (VS 2008)


Hobo Joe
November 11th, 2009, 07:03 PM
C#.NET 2008
How do I make events for custom classes?
There is a VB.NET 2008 example of what I want in C# below.

My example has what I have so far. I don't have any code from me trying to make the event because it didn't work (obviously), so I took it out.

C#.NET 2008

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kill_me_also
{
public partial class Form1 : Form
{
Player Bob;
public Form1()
{
InitializeComponent();
Bob = new Player();
}

private void Form1_Load(object sender, EventArgs e)
{
Bob.Name = "Randazzio";
}

private void Bob_NameChanged()
{
MessageBox.Show("The event worked.", "Yay!!");
}

class Player //Class Your
{
string vName; //Dim vName as String
int vAge; //Dim vAge as Int32
//Public Event TextChanged(ByVal value) 'Example

public string Name //Public Property Name() as String
{
get //Get
{
return vName; //return vName
} //End Get
set //Set(ByVal value as String)
{
vName = value;
} //End Set
} //End Property

public int Age
{
get
{
return vAge;
}
set
{
vAge = value;
}
}

public int AgeIn4Years
{
get
{
return (vAge + 4);
}
}
}
}
}

(Those comments are what the lines VB equivalent would be, if you're wondering)

The code below is in VB.NET 2008. It is how I want the above code to be, but in C# obviously.
[code]
Public Class Form1
Dim WithEvents Bob As Player
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Bob = New Player()
Bob.Name = "Randazzio"
End Sub
Private Sub PlayerName_Changed() Handles Bob.TextChanged
MsgBox("The event worked.", MsgBoxStyle.Information, "Yay!!")
End Sub
Class Player
Private vName As String = ""
Private vAge As Integer
Public Event TextChanged(ByVal value)

Public Property Name() As String
Get
Return vName
End Get
Set(ByVal value As String)
vName = value
RaiseEvent TextChanged(vName)
End Set
End Property

Public Property Age() As Integer
Get
Return vAge
End Get
Set(ByVal value As Integer)
vAge = value
End Set
End Property

Public ReadOnly Property AgeIn4Years() As Integer
Get
Return (vAge + 4)
End Get
End Property
End Class
End Class
[code]

Hobo Joe
November 11th, 2009, 07:05 PM
I don't see an edit button, so here's the VB stuff again:


Public Class Form1
Dim WithEvents Bob As Player
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Bob = New Player()
Bob.Name = "Randazzio"
End Sub
Private Sub PlayerName_Changed() Handles Bob.TextChanged
MsgBox("The event worked.", MsgBoxStyle.Information, "Yay!!")
End Sub
Class Player
Private vName As String = ""
Private vAge As Integer
Public Event TextChanged(ByVal value)

Public Property Name() As String
Get
Return vName
End Get
Set(ByVal value As String)
vName = value
RaiseEvent TextChanged(vName)
End Set
End Property

Public Property Age() As Integer
Get
Return vAge
End Get
Set(ByVal value As Integer)
vAge = value
End Set
End Property

Public ReadOnly Property AgeIn4Years() As Integer
Get
Return (vAge + 4)
End Get
End Property
End Class
End Class

BigEd781
November 11th, 2009, 07:13 PM
class Foo
{
// This is the event
public event EventHandler BarChanged;

// This is the method that actually fires the event.
// it is virtual so that inherited classes can alter the behavior
// or do something before/after the event fires.
protected virtual void OnBarChanged( EventArgs e )
{
if ( BarChanged != null )
{
// fire the event
BarChanged( this, e );
}
}

private int _bar;
public int Bar
{
get { return _bar; }
set
{
if ( _bar != value )
{
_bar = value;
// value changed, alert listeners
OnBarChanged( EventArgs.Empty );
}
}
}
}

static void Main( string[] args )
{
Foo f = new Foo( );
f.BarChanged += f_BarChanged;
f.Bar = 10;
}

static void f_BarChanged( object sender, EventArgs e )
{
Console.WriteLine( "Event was fired" );
}