Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

utilisation de "define"

2 réponses
Avatar
Maurice60
bjr
dans nos projet en c++ on utilisaot bcp ce genre de macro
#define GET__ELEMENT(a) CElement *Courant = ::ListeElements->Elements; \
while(Courant) \
{\
if(dynamic_cast<a *>(Courant))\
if((dynamic_cast<a *>(Courant))->Numero == no)\
return (a *) Courant;\
Courant = Courant->Next;\
}\
return NULL;\

peut-on faire la meme chose en c#?
merci

2 réponses

Avatar
Patrice
Bonjour,

Pas "out of the box". Il doit y avoir éventuellement des preprocesseurs
tiers si l'on veut vraiment faire des macros. Sinon c'est normalement le
compilateur qui est responsable de mettre le code "inline" si il le trouve
opportun.

--
Patrice


"Maurice60" a écrit dans le message de
groupe de discussion :
bjr
dans nos projet en c++ on utilisaot bcp ce genre de macro
#define GET__ELEMENT(a) CElement *Courant = ::ListeElements->Elements;
while(Courant)
{
if(dynamic_cast<a *>(Courant))
if((dynamic_cast<a *>(Courant))->Numero == no)
return (a *) Courant;
Courant = Courant->Next;
}
return NULL;

peut-on faire la meme chose en c#?
merci



Avatar
Christophe Lephay
"Maurice60" a écrit dans le message de
groupe de discussion :
bjr
dans nos projet en c++ on utilisaot bcp ce genre de macro
#define GET__ELEMENT(a) CElement *Courant = ::ListeElements->Elements;
while(Courant)
{
if(dynamic_cast<a *>(Courant))
if((dynamic_cast<a *>(Courant))->Numero == no)
return (a *) Courant;
Courant = Courant->Next;
}
return NULL;

peut-on faire la meme chose en c#?



L'opérateur as fonctionne comme le dynamic_cast (renvoie null si la
conversion échoue).

En mot à mot, ça donne :

if (Courant is a)
if ( (Courant as a).Numero == no)
return (a)Courant;
Courant = Courant.Next;

Sinon, on peut faire la conversion une seule fois :

a* Courant_a = dynamic_cast<a*>(Courant);
if (Courant_a && Courant_a->Numero == no)
return Courant_a;

donnerait en c# :

a Courant_a = Courant as a;
if (Courant_a != null && Courant_a.Numero == no)
return Courant_a;