I have this snippet :
public delegate decimal CalculerMoyenneGenerale(decimal a, decimal b);
Func<decimal, decimal, decimal, decimal> fonc1 = (a, b, c) => (a + b * 2 + c * 2) / 5;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
decimal moyenne1 = decimal.Parse(moy1.Text);
decimal moyenne2 = decimal.Parse(moy2.Text);
decimal moyenne3 = decimal.Parse(moy3.Text);
decimal moyenneBac = decimal.Parse(moybac.Text);
CalculerMoyenneGenerale calcul;
decimal moyenneAnnee = fonc1.Invoke(moyenne1, moyenne2, moyenne3);
calcul = (moyenneAnnee < moyenneBac) ? CalculerSansVightcinq : CalculerAvecVightcinq;
General.Text = calcul(moyenneAnnee, moyenneBac).ToString();
}
public decimal CalculerSansVightcinq(decimal annee, decimal bac)
{
return bac;
}
public decimal CalculerAvecVightcinq(decimal annee, decimal bac)
{
return (bac * 75 + annee * 25 ) / 100;
}
I have a problem in this line :
calcul = (moyenneAnnee < moyenneBac) ? CalculerSansVightcinq : CalculerAvecVightcinq;
it didn't accept this ternary expression ( exception of casting). If I change this expression by ìf else
statement it works.
- Why are the reasons of this exception?
- How can i resolve it?