Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
The creature that won't die
Message
De
25/03/2010 12:45:56
 
 
À
25/03/2010 11:40:53
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
Information générale
Forum:
Visual FoxPro
Catégorie:
VFP Compiler for .NET
Divers
Thread ID:
01456123
Message ID:
01457203
Vues:
75
>>>
LPARAMETERS toObj 
>>>IF PEMSTATUS(toObj, "oGadget", 5)
>>>	toObj.oengine=this.oGadget
>>>ENDIF
      public void Doit(object toObj)
>>       {
>>           PropertyInfo pi = toObj.GetType().GetProperty("oengine");
>>           if (pi != null)
>>               pi.SetValue(toObj,oGadget,null);
>>       }
>
>
>Interesting separation... a property is an object, still related to its owner but you can operate on it all the same. A tad more complicated, but as long as it gets the job done, cool.
>What's the null in the last line for?
That one gets a bit complicated :-} If the property is an indexed one (such as a collection) then the third parameter can be an array of objects specifying which element/s should receive the value....

> And, oGadget isn't defined... shouldn't it be a string literal?
I was assuming oGadget was a property of the class containing the Doit() method - should have written 'this.oGadget' for clarity but the 'this' isn't required.

>
>>>
LPARAMETERS toObj
>>>	IF toObj.BASECLASS="Timer"
>>>		IF toObj.ENABLED AND toobj.interval>0
>>>			toObj.ENABLED=.F.
>>>			toObj.INTERVAL=0
>>>		ENDIF
>>>	ENDIF
>>>
public void Doit (object toObj)
>>        {
>>            Timer t = toObj as Timer;
>>            if (t != null)
>>            {
>>                if (t.Enabled && t.Interval > 0)
>>                {
>>                    t.Enabled = false;
>>                    t.Interval = 0;
>>                }
>>            }
>>        }
>
>The AS operator is unintuitive but neat... implicit cast returning a null if it fails? Though, this sounds more like a try-catch "see if this works" code, instead of having an IsA(toObj, tcClass) function. What happens if you try to cast a twice subclassed timer as a timer, do you still get a reference to the original object?

Yes - you can cast to any ancestor class....
>
>>I'd do a bit more type checking in the first one if it was for production - but even as it stands it is safer than the VFP version...
>
>And what's unsafe in it? In the C# version ?
Firstly, whilst 'pi' not being null guarantees that the property exists and is public it does not guarantee that it is writeable (i.e. not readonly). Also there's no check that the type of the 'oengine' property allows 'oGadget' to be assigned to it. Something like this would be a bit safer:
if (pi != null && pi.CanWrite)
            {
                if (pi.PropertyType == oGadget.GetType())
                    pi.SetValue(toObj, oGadget, null);
            }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform