Event in VBA


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

Windows are build upon three pillars: state, behavior and signal. They are implemented by property, method and events. Events are the way server (producer) signal to client (consumer) that some action has occurred and if desired, now is a good time to respond.
The server defines the events that act as the signature of the place holders and raise the events in the places that clients are permitted to insert their code.
From the client’s perspective, an event is an opportunity to respond to the changing state of an object. The following code is from VB & VBA in a Nutshell: The Language

  • Events can be declared and fired only from within object modules (i.e., Form, User Control, and Class modules). You can’t declare and fire events from a standard code module.
  • Events can be handled or intercepted only from within object modules. You can’t handle any type of event from within a code module. This isn’t really a limitation because you can simply include a call to a function or sub within a code module from within your event handler, to pass program control to a code module–just like you would write code in form and control event handlers.
  • The event declaration must be Public so that it’s visible outside the object module; it can’t be declared as Friend or Private.
  • You can’t declare an object variable as WithEvents if the object doesn’t have any events.
  • To allow the client application to handle the event being fired, the object variable must be declared using the WithEvents keyword.
  • VB custom events don’t return a value; however, you can use a ByRef argument to return a value
  • If your class is one of many held inside a collection, the event isn’t fired to the “outside world”–unless you have a live object variable referencing the particular instance of the class raising the event.
Server code: 
Public Event Warning(sMsg As String, ByRef Cancel As Boolean)
 
Public Property Let ClaimValue(dVal As Double)
 
   Dim blnCancel As Boolean
 
   If dVal > 100 Then
      RaiseEvent Warning("The Claim Value appears high", _
                         blnCancel)
      If blnCancel Then
         Exit Property
      End If
   End If
 
   mdClaimValue = dVal
 
End Property
 
Client code: 
Private WithEvents oServer As clsServer
 
Private Sub cmdAdd_Click()
    Set oServer = New clsServer
    oServer.ClaimValue = 999
End Sub
 
Private Sub oServer_Warning(sMsg As String, _
                            Cancel As Boolean)
    Dim iResponse As Integer
    iResponse = MsgBox(sMsg & " is this OK?", _
                       vbQuestion + vbYesNo, _
                       "Warning")
    If iResponse = vbNo Then
        Cancel = True
    Else
        Cancel = False
    End If
 
End Sub

LINQ to SQL return a typed class/struct


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

The trick is to project the query result into an ordinary named type with an object initializer:
Paginate is used to demonstrate how the query can be used for further processes.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace LINQPart3_CSharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int startRow = 10;
            int takeRow = 20;
 
            GridView1.DataSource = BindProducts(startRow, takeRow);
            GridView1.DataBind();
        }
 
        private IQueryable
 BindProducts(int startRow, int takeRow)
        {
            NorthwindDataContext db = new NorthwindDataContext();
 
            var products = from p in db.Products
                           where p.OrderDetails.Count >2
                           select new prod
                           {
                               ID = p.ProductID,
                               Name = p.ProductName,
                               NumOrders = p.OrderDetails.Count,
                               Revenue = String.Format("{0:C}", p.OrderDetails.Sum(o => o.UnitPrice * o.Quantity))
                           };
            //return products.Skip(startRow).Take(takeRow);
            return Paginate(products, startRow, takeRow);
        }
 
        public IQueryable Paginate(IQueryable query, int skip, int take)
        {
            return query.Skip(skip).Take(take);
        }
 
        struct prod {
            public int ID { get; set; }
            public string Name { get; set; }
            public int NumOrders { get; set; }
            public string Revenue { get; set; }
        }
    }
}

A Simple LinqToObjects Snippet


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace LinqToObjects
{
    class LinqToObjects
    {
        static void Main(string[] args)
        {
            string[] names = { "James Huddleston", "Pearly", "Ami Know", "Rupli Agarwal", "Beth Christams", "Fabio Claudio", "Vamika Agarwal", "Vidya Varat Agrawaaaa"};
            IEnumerable<string> namesOfPeople =             
                from name in names
                where name.Length >9
                select name;
            foreach (var name in namesOfPeople)
            {
                Console.WriteLine(name);
            }
        }
    }
}