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]
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]