Saturday, June 28, 2008

Code Template - Property with Getter & Setter

I was fiddling with a code template I'd written [or more likely, "borrowed" from somewhere] which I've been using for completing Read-Write and Read-Only properties. Originally it was set up with two points ... one for the property variable ["ident"] and one for it's type ["type"].

When setting the "ident" variable point, the template was automatically filling in the Getter and Setter following the "F" for me and prior to using Code Completion [Ctrl-Shift-C] I would have to make sure I replaced the "F" with either Get or Set if I wanted actual Getter or Setter methods.

This original CDATA:
<![CDATA[property |ident|: |type| read F|ident|;|end|]]>
produces this code:
property Test: Integer read FTest write FTest;

I didn't know if sticking two point variable side-by-side would work. I can't imagine why it wouldn't, but you never know. So, I experimented and sure enough adding a couple of "setter" and "getter" points and having them that close to another point in the CData - wasn't a problem at all.

Now after setting [or leaving the default] property type, it's either Tab to leave the default "Get" or "Set" -or- replace the appropriate one [or both] with "F".

Now, it's as simple as:

"proprw [tab/space] Test [tab][tab][tab[tab]" to get:
property Test: Integer read GetTest write SetTest;

- or -

"proprw [tab/space] Test [tab][tab] F [tab] F [tab] to get:
property Test: Integer read FTest write FTest;

Code templates simply rock! A little front end work, to accomplish a lot of back end work ... much, much faster.

Here's the modified version that I use for a read-write property ... enjoy. Note that I've changed the point names to something meaningful ... "ident" wasn't really all that descriptive.

<?xml version="1.0" encoding="utf-8" ?> 
<codetemplate    xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0"> 
    <template name="proprw" invoke="auto"> 
        <description>Property with Getter and Setter.</description> 
        <author>tdh</author> 
        <point name="Name"> 
            <text>Name</text> 
            <hint>the Name for the property</hint> 
        </point> 
        <point name="Type"> 
            <text>Integer</text> 
            <hint>the Type for the property</hint> 
        </point> 
        <point name="Getter"> 
            <text>Get</text> 
            <hint>the property Getter</hint> 
        </point> 
        <point name="Setter"> 
            <text>Set</text> 
            <hint>the property Setter</hint> 
        </point> 
        <code language="Delphi" delimiter="|"> 
        <![CDATA[property |Name|: |Type| read |Getter||Name| write |Setter||Name|;|end|]]> 
        </code> 
    </template> 
</codetemplate>

Copy the code above into a text file, and save it as:
C:\My Documents\RAD Studio\code_templates\PropertyGetSet.xml
I'll leave creating the Read-Only derivative to you.

Note: I had a look at the Delphi Wiki and did find one where the author was "twm". It's probable that's where I got the original that I was working with so apologies to "twm" if it appears that I'm taking credit for your work - last thing I want to do, honestly.

Thanks for stopping by ...
Dave

Friday, June 13, 2008

Essential Reading ... Essential Pascal

Back from a extremely busy [which fortunately, has the advantageous effect of making it seemingly short] 28 day stint at sea, I was pleased to see that my copy of Essential Pascal by Marco Cantù had arrived. In the fog of work, which had virtually hidden the real world from memory, I had actually forgotten all about having ordered it.

Although I had downloaded, printed and read one of the earlier PDF versions a few years back, I found it a pleasure to have an official dead tree version, in hand, to have another look at. The book is a very easy read and, in my opinion, simply a must have for anyone from newbies to masters of other languages, who is new to writing Pascal code. Although speaking to the needs of masters is admittedly, quite a bit beyond my skill level.

For the programming newbie, there are a couple of areas where I feel it will be a bit of a grunt to get your head around the concepts. Fortunately, the fact that you don't fully understand them isn't likely to impede you in getting up and running with Pascal code. The two specific areas that I thought that it may be a bit of a struggle for the newbie to get a handle on, are Pointers and Parameters.

Although you could probably write Delphi code forever without even thinking about pointers, you would be doing yourself a serious injustice if you did. They're sitting there staring you in the face and you should at least understand how they work. The concept of pointers, to put it simply, is initially a bit of a bugger - it certainly was for me. Once you "get it" you do look back and wonder what all the fuss was about though. To get over the Pointers hump, in addition to what Marco has presented in the book, have a look at:

As far as the understanding of parameters is concerned, Marco has thankfully explained the concepts in order of complexity. Take it a bit at a time, read the text, then have a look at Movie #34 - Parameters by Alister Christie at CodeGearGuru.com to get the basics [there's some revealing info on pointers in that movie as well]. Understanding that, by default, you're passing parameters by value and what happens when these values get changed, is an essential concept. Knowing that you can, and why you would want to, pass them either by reference or as constant is very important. Finally, although you may never contemplate writing a routine that passes either a "Open Array" or a "Type Variant Open Array" parameter, it is very likely that some where down the line, you will use one. Understanding what you're feeding in, will make it quite a bit easier to understand the results you get. Parameters can be very confusing, specifically when you start passing objects. I'm still trying to figure it out fully - maybe once I do, I'll share that information here. It'll all boil down to a few basic rules that are easy to follow and remember - like pointers, once you get it, you're good.

Essential Pascal by Marco Cantù is a book you simply need to read and have at hand in one form or another. As a newbie, thinking that you can just read the help file and figure out how to write Pascal code [fodder for another post - at another time] will just lead to frustration. Believe me, I looked at this as an option from a "Why not just ... " perspective before writing this post and it is certainly not the way I would want to have to do it.

Thanks for stopping by ...
Dave