Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Naming conventions for custom methods?
Message
 
 
À
19/11/2008 13:48:52
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Divers
Thread ID:
01362229
Message ID:
01362898
Vues:
27
>>>>OK, now I see what you are getting at. Typically a class contains something called a property which reads and/or writes each piece of data defined by the class. You update a value by calling its Set method, and retrieve it by calling its Get method.
>>>
>>>IOW, you can't access the properties directly as you do in VFP, but need code for each of them. Not sure I like that, either, but that's a different story. "Typically"? I.e. are there exceptions?
>>>
>>
>>Sure, you can do it the old way by coding a Get method and a Set method. The main difference is you reference a property like a field rather than a method, i.e. no parentheses. As far as writing code, yes, you do have to write it, but IntelliSense does most of the work for you. Type "prop", press Tab and Enter, and it generates a stub. You change the data type and field name and it propogates those. It's a very simple, quick process.
>
>Now a piece of code can access a property .blabla directly only if the name of the piece is .blabla_set or .blabla_get? And nothing else can access them unless such pieces of code exist? For every knowing property out there you need to WRITE code? And the code is something like
>
>proc blabla_get()
>return this.blabla
>
>proc blabla_set(tuVal)
>this.blabla=tuVal
>
>C'mon, this couldn't have been a default? Couldn't have had a couple of bits somewhere, declare the property hidden or public or something if you want it (in)accessible? To me, this is the most unproductive side of it, even if it's for custom properties only (which I don't know). Needlessly multiplies the amount of code, creates a bunch of places where a bug can creep in - among hundreds of such generated snippets, one contains an accidental typo so it returns this.num instead of this.bum)
>
> Sheesh... "It's a very simple, quick process" sounds like something a dentist may tell you ;).

Here is a sample property, which would exist within a class definition:

private decimal weight;
public decimal Weight
{
get { return weight; }
set { weight = value; }
}

Let's say this property is part of a class called human and we instantiate it as Mike. So the references go like this:

Mike.Weight = 198;
decimal curWeight = Mike.Weight;

The runtime knows whether to use the get or set accessor based on the calling syntax.

If we were not using properties there would probably be class methods called weight_get() and weight_set(), with the latter being called as weight_set(198). The result is exactly the same, the calling syntax is just a little terser.

This was a very simple example. You can also code more complicated logic -- to validate input values, for example -- within the get and set blocks.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform