Soyez le premier à donner votre avis sur cette source.
Snippet vu 6 228 fois - Téléchargée 34 fois
//Créé par DeadlyPredator //Je ne peux être tenu responsable de tout dommage ou perte éventuellement causé directement ou indirectement par ce code. //À COLLER DANS UN FICHIER APPELÉ uOptionContainer.pas unit uOptionContainer; interface type TProperty=packed record strName, strValue: String; end; PProperty=^TProperty; type TAnObject=packed record strName: String; iPropCount: Integer; axProperties: array of TProperty; end; PAnObject=^TAnObject; procedure ModifyProperty(strName, strValue: String; xObj: PAnObject); function GetPropertyValue(strName: String; xObj: PAnObject;out bErr: Boolean):String; procedure DeleteProperty(strName: String; xObj: PAnObject); implementation procedure ModifyProperty(strName, strValue: String; xObj: PAnObject); var I: Integer; bOK: Boolean; begin bOk:=False; if(xObj.iPropCount>0) then begin for I:=0 to (xObj.iPropCount-1) do begin if((xObj.axProperties[I].strName=strName) or (xObj.axProperties[I].strName=#0)) then begin if(xObj.axProperties[I].strName=#0) then xObj.axProperties[I].strName:=strName; xObj.axProperties[I].strValue:=strValue; bOK:=True; exit; end; end; end; if not bOk then begin xObj.iPropCount:=xObj.iPropCount+1; SetLength(xObj.axProperties,xObj.iPropCount); xObj.axProperties[xObj.iPropCount-1].strName:=strName;xObj.axProperties[xObj.iPropCount-1].strValue:=strValue; end; end; function GetPropertyValue(strName: String; xObj: PAnObject;out bErr: Boolean):String; var I: Integer; bOK: Boolean; begin bOk:=False; if(xObj.iPropCount>0) then begin for I:=0 to (xObj.iPropCount-1) do begin if(xObj.axProperties[I].strName=strName) then begin GetPropertyValue:=xObj.axProperties[I].strValue; bOK:=True; bErr:=False; exit; end; end; end; if not bOk then begin GetPropertyValue:=#0; bErr:=True; end; end; procedure DeleteProperty(strName: String; xObj: PAnObject); var I: Integer; begin if(xObj.iPropCount>0) then begin for I:=0 to (xObj.iPropCount-1) do begin if(xObj.axProperties[I].strName=strName) then begin xObj.axProperties[I].strName:=#0; xObj.axProperties[I].strValue:=#0; exit; end; end; end; end; end. //EXEMPLE D'UTILISATION procedure PlacerProprietes(); var MonSetDeProps: TAnObject;//Le containeur de propriétés bErr: Boolean;//Requis pour s'assurer qu'il n'y a pas de problèmes MaValeur: String;//Exemple d'une façon de stocker le résultat begin //Initialisation de la structure MonSetDeProps.iPropCount:=0; //!!!Losrque vous devez passer la structures aux fonctions comme argument, utilisez son pointeur (@) //Ajout de valeurs dans MonSetDeProps ModifyProperty('Valeur inutile','?!?!?',@MonSetDeProps); ModifyProperty('Valeur inutile2','!?!?!',@MonSetDeProps); ModifyProperty('Taille','100 m',@MonSetDeProps); ModifyProperty('Âge','27 ans',@MonSetDeProps); //Supression de valeurs dans MonSetDeProps DeleteProperty('Valeur inutile',@MonSetDeProps); DeleteProperty('Valeur inutile2',@MonSetDeProps); //Suite de l'ajout de valeurs dans MonSetDeProps ModifyProperty('Prix','20$',@MonSetDeProps); ModifyProperty('Nombre','123456',@MonSetDeProps); //Obtention de valeurs dans MonSetDeProps //Si la propriété n'existe pas, bErr retourne TRUE MaValeur:=GetPropertyValue('Taille',@MonSetDeProps,bErr); ShowMessage(MaValeur);//Utiliser la valeur... MaValeur:=GetPropertyValue('Nombre',@MonSetDeProps,bErr); ShowMessage(MaValeur);//Utiliser la valeur... MaValeur:=GetPropertyValue('Âge',@MonSetDeProps,bErr); ShowMessage(MaValeur);//Utiliser la valeur... MaValeur:=GetPropertyValue('Prix',@MonSetDeProps,bErr); ShowMessage(MaValeur);//Utiliser la valeur... //Gestion d'erreur MaValeur:=GetPropertyValue('VALEUR_INEXISTANTE!',@MonSetDeProps,bErr); if bErr then ShowMessage('Propriété introuvable!'){gèrer le problème} else ShowMessage(MaValeur);{Utiliser la valeur...} ShowMessage(IntToStr(MonSetDeProps.iPropCount)+' entées'); end;
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.