Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Define Custom Event
Message
 
À
16/02/2009 18:39:56
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01382215
Message ID:
01382229
Vues:
36
Thanks for the example. My problem with events has always been the syntax. I don't understand what all this is, nor how it works. Could you break all this down for me?



>>One of the two areas in C# I'm still foggy in is Events. I have a need for an event, and a practicle example is a good learning tool.
>>
>>I am interating through a dataset, and I want to raise an event for each row. I'm unsure as to what goes here:
>>
>>
>>foreach (DataRow oRow in MyData.Tables[0].Rows)
>>{
>>
>>}
>>
>>
>>
>>Can someone help a newbie?
>
>Here is a sample (added dataset just because you had one):
>
>using System;
>using System.Collections.Generic;
>using System.Linq;
>using System.Text;
>using System.IO;
>using System.Data;
>using System.Data.Sql;
>using System.Data.SqlClient;
>
>namespace ConsoleApplication1
>{
>    class MySample
>    {
>        public delegate void MyRowHandler(DataRow r);
>        public event MyRowHandler MyEvent;
>
>        static void Main(string[] args)
>        {
>            MySample s = new MySample();
>            s.MyEvent += new MyRowHandler(MyHandler1);
>            s.MyEvent += new MyRowHandler(MyHandler2);
>            s.MyEvent += oRow => 
>                Console.WriteLine("Lambda expression call to my event handler: {0}",
>                oRow.Field<string>("CompanyName"));
>
>            DataSet MyData = new DataSet();
>            SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;trusted_connection=yes;database=Northwind");
>            SqlCommand cmd = new SqlCommand("select * from customers where country = 'USA'", cn);
>            cn.Open();
>            SqlDataReader rdr = cmd.ExecuteReader();
>            MyData.Tables.Add();
>            MyData.Tables[0].Load(rdr);
>            cn.Close();
>
>            foreach (DataRow oRow in MyData.Tables[0].Rows)
>            {
>                if (s.MyEvent != null)
>                    s.MyEvent(oRow);
>            }
>
>
>        }
>
>        private static void MyHandler1(DataRow row)
>        {
>            Console.WriteLine("MyHandler1 called: {0}", row[0].ToString());
>        }
>        private static void MyHandler2(DataRow row)
>        {
>            Console.WriteLine("MyHandler2 called: {0}", row[0].ToString());
>        }
>    }
>}
Cetin
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform