CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Cutoms classes and events - I can't get the events to work (VS 2008)

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

  2. #2
    Join Date
    Nov 2009
    Posts
    2

    Re: Cutoms classes and events - I can't get the events to work (VS 2008)

    I don't see an edit button, so here's the VB stuff again:

    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

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Cutoms classes and events - I can't get the events to work (VS 2008)

    Code:
    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" );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured