PDA

Ver la Versión Completa : Operador IS


Aztaroth
23-02-2004, 23:05:18
Hay algun sustituto en Builder para el operador "is" de delphi

ej :

if IdMsgSend.MessageParts.Items[idx] is TIdAttachment then

como seria la implementacion en BCpp 6

Gracias.

xeroq
31-07-2004, 23:46:07
Primero,todos las clases de la VCL en C++ Builder deben crearse exclusivamente como punteros,por compatibilidad con Object Pascal.

Para poder hacer lo mismo sería:

if (dynamic_cast<TButton *>(Sender)!=NULL)
hacer_algo;

Te diria si Sender es un TButton

barman
02-08-2004, 10:42:05
No se si es a lo que te refieres

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Sender ->ClassNameIs("TButton"))
ShowMessage("Boton");
}

jachguate
02-08-2004, 17:59:47
No se si es a lo que te refieres

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Sender ->ClassNameIs("TButton"))
ShowMessage("Boton");
}

Pero un TButton es un TControl... y:


void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Sender ->ClassNameIs("TControl"))
ShowMessage("Control");


nos diría que no lo es... :( cuando el operador IS en delphi, nos diría (correctamente) que si lo es.

Hasta luego.

;)

barman
03-08-2004, 12:18:58
Lo que quieres saber es la clase padre,

TClass ClassRef;
ClassRef = Sender ->ClassType();
ClassRef = ClassRef->ClassParent();
ShowMessage(String(ClassRef->ClassName()));

Tendrias que hacerte un bucle hasta que ClassRef == NULL y te iria diciendo todos los atecesores, No se me ocurre otra cosa.

barman
03-08-2004, 12:21:29
Gueno, blanco y migao

TClass ClassRef;
ClassRef = Sender ->ClassType();
ClassRef = ClassRef->ClassParent();
while (ClassRef != NULL)
{
ShowMessage(String(ClassRef->ClassName()));
ClassRef = ClassRef->ClassParent();
}

jachguate
03-08-2004, 20:39:49
me gusta mas el dynamic_cast... :D

Hasta luego.

;)

barman
04-08-2004, 09:51:56
A, si , bueno, vale, pero, tal vez, quiza, no se, ¿Cual era la pregunta?,
el codigo es para saber todos los antecesores de un componente, no tengo ni idea de los que hace el is de delphy.
Y ya ta.

jachguate
04-08-2004, 14:44:27
¿Cual era la pregunta? :confused:


no tengo ni idea de los que hace el is de delphy

mejor dejo que el propio delphi se explique:

The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object. The expression

object is class

returns True if object is an instance of the class denoted by class or one of its descendants, and False otherwise. (If object is nil, the result is False.) If the declared type of object is unrelated to class—that is, if the types are distinct and one is not an ancestor of the other—a compilation error results. For example,

if ActiveControl is TEdit then TEdit(ActiveControl).SelectAll;

This statement casts a variable to TEdit after first verifying that the object it references is an instance of TEdit or one of its descendants.

De esta cuenta, que las siguientes operaciones resultarian en:

siendo miGrid un TDBGrid:

miGrid is TDBGrid (true) miGrid si es un TdbGrid
miGrid is TCustomControl (true) miGrid si es un TdbGrid
miGrid is TComponent (true) miGrid si es un TdbGrid
miGrid is TObject (true) miGrid si es un TdbGrid
miGrid is TStringGrid (falso) miGrid no es un TStringGrid


Hasta luego.

;)