Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trying To Understand Events
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Trying To Understand Events
Divers
Thread ID:
01382673
Message ID:
01382673
Vues:
95
I am trying to understand events. Coming from VFP where there are no user-definabnle events, it's a bit of a curve for me.

I wrote this class which returns a SqlDataReader, and which contains an event:
namespace Events
{
    public delegate void ReaderRetreivedEvent();

    class BankAccounts
    {
        public event ReaderRetreivedEvent ReaderRetreived;

        private Exception _oException = null;
        public Exception oException
        {
           get { return _oException; }
        }

        public SqlDataReader GetReader(string sQuery)
        {
            SqlConnection oConn = _ConnectToDb(); // Not included here for brevity

            SqlCommand oCommand = new SqlCommand();
            SqlDataReader oReader = null;

            oCommand.CommandText = sQuery;
            oCommand.Connection = oConn;

            try
            {
                oReader = oCommand.ExecuteReader();
            }
            catch (SqlException e)
            {
                _oException = e;
            }

            if (_oException == null)
            {
                ReaderRetreived();
            }

            return oReader;

        }

    }
}
I'm trying to test it like this:
class Program
{
    static SqlDataReader oReader = null;

    static void Main(string[] args)
    {
        BankAccounts oBank = new BankAccounts();
        oReader = oBank.GetReader("select * from registers");

        oBank.ReaderRetreived += new ReaderRetreivedEvent(ReaderRetreived);  // COMPILE ERROR HERE

    }

    private void ReaderRetreived()
    {
        while (oReader.Read())
        { 
            // Some code here
        }
    }
}
I'm getting a compile time error
"An object reference is required for the nonstatic field, method, or property 'Events.Program.ReaderRetreived():

What object is it expecting???
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform