J'essaie actuellement de m'apprendre à utiliser les événements, mais je n'arrive jamais à faire en sorte que tout fonctionne correctement.Événements d'apprentissage
La classe à l'événement de codage ressemble à ceci:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventsPractice
{
public delegate void myEventHandler(string newValue);
public class EventExample
{
private string _value;
public event myEventHandler valueChanged;
public string val
{
set
{
_value = value;
valueChanged(_value);
}
}
static void myEvt_valueChanged(string newValue)
{
Console.WriteLine("The value changed to {0}", newValue);
}
}
}
Ma classe de programme ressemble à ceci:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventsPractice;
namespace EventsPractice
{
public class Program
{
static void Main(string[] args)
{
EventExample myEvt = new EventExample();
myEvt.valueChanged += myEventHandler(myEvt_valueChanged);
string str;
do
{
str = Console.ReadLine();
if (!str.Equals("exit"))
myEvt.val = str;
} while (!str.Equals("exit"));
}
}
}
Les erreurs que je reçois sont:
« Erreur 1 ' EventsPractice.myEventHandler 'est un' type 'mais est utilisé comme une' variable 'C: \ Users \ Administrateur \ Documents \ Visual Studio 2010 \ Projects \ EventsPractice \ EventsPractice \ Program.cs 15 35 ÉvénementsPractice "
Erreur 2 Le nom 'myEvt_valueChanged' n'existe pas dans le contexte actuel C: \ Users \ Administrateur \ Documents \ Visual Studio 2010 \ Projects \ EventsPractice \ EventsPractice \ Program.cs 15 50 EventsPractice
Je ne suis pas sûr ce qui me manque ou où je me suis trompé. Toute aide serait appréciée.