PROGRAMMING POCKET POSTCODE WITH .NET
The Postcode Class (afdpc.vb/afdpc.cs)
For VB and C# .NET developers full compatibility is provided with our core API using the afdpc.vb and afdpc.cs classes provided. These provide direct access to the Pocket Postcode API when using .NET and the classes isolate all the complexities of calling structure based native Windows API's in the .NET environment, while still providing the full efficiency of our fast native address engines.
For VB .NET developers:
The afdpc.vb class is, by default, located in your c:\program files\microsoft
activesync\pocket postcode\examples\vbnet\afdpc.vb. If you open postcode.sln in
that folder an example
of how to use the class is also provided.
To use this class in your own application copy it to your source code folder for your project. Then with your project open in Visual Studio .NET, click the Project menu, select Add Existing Item, then double click the afdpc.vb class to add - it is now ready to use in your project.
For C# .NET developers:
The afdpc.cs class is, by default, located in your c:\program files\microsoft
activesync\pocket postcode\examples\csharp\afdpc.cs. If you open
postcode.sln in that folder an example of how to use the class is also provided.
To use this class in your own application copy it to your source code folder for your project. Then with your project open in Visual Studio .NET, click the Project menu, select Add Existing Item, then double click the afdpc.cs class to add - it is now ready to use in your project.
Carrying out a Lookup (for Postcode or lookup string)
To carry out a lookup, first create an instance of the afdpc class if you have not already done so, for example:
VB .NET Dim pc As afdpc
pc = New afdpcC# .NET afdpc pc;
pc = new afdpc();Next, set the lookup property of the afdpc class instance with the string you wish to lookup, e.g. 'B11 1AA':
VB .NET pc.Lookup = "B11 1AA" C# .NET pc.Lookup = "B11 1AA"; Now you can set the county option, e.g. the county type that you wish to return (if desired). By default no county is returned and it should be noted that Counties are no longer considered part of a full postal address. The options available are as follows:
- "0" - Omit County (County return property will always be blank)
- "1" - Postal County (County return property will give the former Royal Mail Postal County if there is one)
- "2" - Abbreviated Postal County (County return property will give the former Royal Mail Postal County if there is one, it will also be abbreviated to any official Royal Mail abbreviation if one exists)
- "3" - Postal Optional County (County return property will give the Royal Mail Postal County including for towns where the County was not formally used postally, the county should only be blank for very large cities or for where no sensible county exists).
- "4" - Abbreviated Postal Optional County (County return property will give the Royal Mail Postal County including for towns where the County was not formally used postally, it will also be abbreviated to any official Royal Mail abbreviation if one exists).
- "5" - Traditional County (County return property will give the traditional county name for this town if one exists).
- "6" - Administrative County (County return property will give the administrative county name for this town if one exists - not usually used for addresses).
This sets the county option to Postal Optional County, however any of the values above could be used instead for the CountyOption property:
VB .NET pc.CountyOption = "3" C# .NET pc.CountyOption = "3"; The next step is to call the GetFirst function to return the first matching result for this lookup:
VB .NET retVal = pc.GetFirst(afdpc.LOOKUP_MODE + afdpc.OUTCODE_BREAKS) C# .NET retVal = pc.GetFirst(afdpc.LOOKUP_MODE + afdpc.OUTCODE_BREAKS); The afdpc.LOOKUP_MODE constant tells the GetFirst function that a lookup is being carried out.
The afdpc.OUTCODE_BREAKS constant is optional and using it means that with lookup's other than for a postcode, the GetFirst function will return afdpc.RETURN_OUTCODE_BREAK (5) when it reaches the end of an outcode (first part of the postcode before the space, e.g. B11) for the lookup. This is recommended as it allows the user to cancel in the middle of a lookup and stops your application appearing to have "hung" in the middle of a long lookup.
The afdpc.OUTCODE_SKIP constant can also be added to this if you only require the first result per outcode to be returned from your lookup, this is considered advanced functionality and would not be useful to most users.Next you can examine the return code for any error and then process the results returned. This is the same for lookup's and searches and is given in the Examining Results section which follows:
Examining Results from a Lookup or Search
Before using any of the properties now available following this lookup, you first need to check the return code for any error. If the return value (retVal above) is less than zero an error has occurred. This error could be any of the following:
- afdpc.RETURN_NOT_FOUND - No Records were found matching the lookup or search criteria given.
- afdpc.RETURN_ERROR_OPENING_FILES - Postcode was unable to open it's data files. Please check the software is installed correctly.
- afdpc.RETURN_DATA_LICENCE_ERROR - Postcode was not correctly registered. Please run the front-end to register.
- afdpc.RETURN_NO_SEARCH_DATA - No lookup or search data was specified, i.e. the Lookup property was not set or the Search criteria was all blank.
- afdpc.RETURN_INVALID_LOOKUP_TYPE - An invalid lookup type was given, i.e. you did not pass the value afdpc.LOOKUP_MODE or afdpc.SEARCH_MODE to the GetFirst function or if you did it was summed with another value that is invalid.
If the return value is not negative an error has not occurred.
You can retrieve more friendly error text messages by calling the ErrorText function of the object, this takes the return value (retVal in this case) as a parameter and returns a string description of the error. For example code to display this and exit the subroutine to abort the lookup would be as follows:
VB .NET If retVal < 0 Then
MsgBox(pc.ErrorText(retVal), MsgBoxStyle.Critical, "Error")
Exit Sub
End IfC# .NET if (retVal < 0)
{
MessageBox.Show(afdpc.ErrorText(retVal));
return;
}You may also wish to inform the user if the postcode entered as there lookup or search has changed and therefore the results that are returned will include the new data. This can also be done by checking the return code. If the result code is afdpc.RETURN_CHANGED_POSTCODE then The postcode has changed and the Postcode property (i.e. pc.Postcode in this case) will hold the new postcode.
An example of how to display this information to the user is given below:
VB .NET If retVal = afdpc.RETURN_CHANGED_POSTCODE Then
MsgBox("The postcode you entered has changed to: " + pc.Postcode, MsgBoxStyle.Information, "Changed Postcode")
End IfC# .NET if (retVal == afdpc.RETURN_CHANGED_POSTCODE)
{
MessageBox.Show("The postcode you entered has changed to: " + pc.Postcode);
}Next you can do something with this and call off all other results using the GetNext() function:
VB .NET Do While retVal >= 0
If retVal <> afdpc.RETURN_OUTCODE_BREAK Then
' Do something with this result
End If
retVal = pc.GetNext()
LoopC# .NET while (retVal >= 0)
{
if (retVal != afdpc.RETURN_OUTCODE_BREAK)
{
// Do something with this result
}
retVal = pc.GetNext();
}Please note that in most cases with a single Postcode lookup only one result will be returned, however there are postcodes which do have more than one street on them and other types of lookup and search are quite likely to return multiple results.
GetNext could return any of the following:
- afdpc.END_OF_SEARCH - This indicates that the lookup or search has completed and there are no further records to return - no new record has been returned by this call.
- afdpc.RETURN_MAX_LIMIT_REACHED - This indicates that more than the maximum allowable (300) records have been returned from a single lookup or search. This would not happen in the case of a single Postcode lookup and with other lookup's would indicate that the criteria is far too vague to be useful.
- afdpc.RETURN_OUTCODE_BREAK - This is also a possible return value from GetFirst and simply means that the end of an outcode (the part of the postcode before the space) being searched has been encountered and is intended to give the user the chance to cancel the lookup if desired. It cannot occur with single postcode lookup's.
For each result you can read all or any of the afdpc class instance properties to do something with the data that is returned. These properties are as follows:
Property Description Postcode The Postcode PostcodeType The type of Postcode, i.e. Large user (returns LU) or Small User (returns SU). Organisation The Organisation. Please note that Postcode does not contain Organisation data so this will only be present if an obvious organisation was included in the lookup, e.g. 'AFD Software Ltd, IM8 1RF' Prop The Property. Please note that Postcode does not contain Property data so this will only be present if additional non-numeric data has been included in the lookup, e.g. 'The Bungalow, B11 1AA'. Street The Street Locality The Locality Town The Town County The County (in the type specified by the CountyOption you supplied prior to calling GetFirst) CountyOption The Option you specified for the County prior to starting the lookup. Mailsort The Mailsort Code StdCode The likely STD Code for this address GridN The Grid Reference Northing - blank for non-Plotter users GridE The Grid Reference Easting - blank for non-Plotter users CensusCode The Censation Census Code for this Postcode Affluence The Affluence Description for this Postcode LifeStage The LifeStage Description for this Postcode AdditionalCenusInfo Additional Census Information for this Postcode For example to assign the Street to a text box called Street on your form you would use the following code:
VB .NET txtStreet.Text = pc.Street C# .NET txtStreet.Text = pc.Street; If you wish to display the result in a list box you can also retrieve a list box friendly single string by using the ListStr method of the Listbox. The ListStr method of the afdpc class instance simply returns a string. For example the following code would add the result to a list box called lstResults on your form:
VB .NET lstResults.Items.Add(pc.ListStr()) C# .NET lstResults.Items.Add(pc.ListStr()); If you wish to store a result for later display then call the ToString() method and store this in a string array. You can then make an instance of the afdpc class to retrieve it by using the new(string) override method of the afdpc class. For example the following code stores the result in a string and then makes a new instance of the class from it:
VB .NET ' method to store result in a string
Dim storeResult As String
storeResult = pc.ToString()
' later retrieval in new method
Dim pc As afdpc
pc = New afdpc(storeResult)C# .NET // method to store result in a string
string storeResult = pc.ToString();
// later retrieval in new method
afdpc pc;
pc = new afdpc(storeResult);
You should note that Postcode has limitations in it's search functionality. This is because, unlike our higher end products such as Postcode Plus, Postcode does not contain property data. This means that it is often not possible to determine an individual Postcode for a street address as many streets contain more than one Postcode - however the ability to search and then narrow down the possibilities can often be useful and, of course, searching can have many other uses other than locating the correct address.
To carry out a search, first create an instance of the afdpc class if you have not already done so, for example:
VB .NET Dim pc As afdpc
pc = New afdpcC# .NET afdpc pc;
pc = new afdpc();Next you need to set one or more of the search properties. You should set all properties that you don't use to a blank string, as unless this is a newer created object instance old address elements could exist in some of them. The properties available for searching are as follows:
Property Description Postcode A Postcode or partial Postcode to search for Organisation Please note that Postcode does not contain Organisation data so any organisation supplied will simply be returned for all results and cannot be matched to individual records. Prop Please note that Postcode does not contain Property data so any property supplied will simply be returned for all results and cannot be matched to individual records. Street The Street to search for Locality The Locality to search for Town The Town to search for County The County to search for - all county types are searched for this. StdCode The STD Code to search for You should note that a search must include at least part of either the Postcode, Town, County or StdCode. The most common search is for a Street and Town together.
An example of how to set the search criteria to search for Commerical Street in Birmingham is as follows:
VB .NET pc.Organisation = ""
pc.Prop = ""
pc.Street = "Commercial Street"
pc.Locality = ""
pc.Town = "Birmingham"
pc.County = ""
pc.Postcode = ""
pc.StdCode = ""C# .NET pc.Organisation = "";
pc.Prop = "";
pc.Street = "Commercial Street";
pc.Locality = "";
pc.Town = "Birmingham";
pc.County = "";
pc.Postcode = "";
pc.StdCode = "";Now you can set the county option, e.g. the county type that you wish to return (if desired). By default no county is returned and it should be noted that Counties are no longer considered part of a full postal address. The options available are as follows:
- "0" - Omit County (County return property will always be blank)
- "1" - Postal County (County return property will give the former Royal Mail Postal County if there is one)
- "2" - Abbreviated Postal County (County return property will give the former Royal Mail Postal County if there is one, it will also be abbreviated to any official Royal Mail abbreviation if one exists)
- "3" - Postal Optional County (County return property will give the Royal Mail Postal County including for towns where the County was not formally used postally, the county should only be blank for very large cities or for where no sensible county exists).
- "4" - Abbreviated Postal Optional County (County return property will give the Royal Mail Postal County including for towns where the County was not formally used postally, it will also be abbreviated to any official Royal Mail abbreviation if one exists).
- "5" - Traditional County (County return property will give the traditional county name for this town if one exists).
- "6" - Administrative County (County return property will give the administrative county name for this town if one exists - not usually used for addresses).
This sets the county option to Postal Optional County, however any of the values above could be used instead for the CountyOption property:
VB .NET pc.CountyOption = "3" C# .NET pc.CountyOption = "3"; The next step is to call the GetFirst function to return the first matching result for this lookup:
VB .NET retVal = pc.GetFirst(afdpc.SEARCH_MODE + afdpc.OUTCODE_BREAKS) C# .NET retVal = pc.GetFirst(afdpc.SEARCH_MODE + afdpc.OUTCODE_BREAKS); The afdpc.SEARCH_MODE constant tells the GetFirst function that a lookup is being carried out.
The afdpc.OUTCODE_BREAKS constant is optional and using it means that with lookup's other than for a postcode, the GetFirst function will return afdpc.RETURN_OUTCODE_BREAK (5) when it reaches the end of an outcode (first part of the postcode before the space, e.g. B11) for the lookup. This is recommended as it allows the user to cancel in the middle of a lookup and stops your application appearing to have "hung" in the middle of a long lookup.
The afdpc.OUTCODE_SKIP constant can also be added to this if you only require the first result per outcode to be returned from your lookup, this is considered advanced functionality and would not be useful to most users.Next you can examine the return code for any error and then process the results returned. This is the same as for the lookup's and you should refer to the Examining Results section for details of how to do this.
The following is a complete list of all the constants found in the afdpc class.
Constant Description Return Codes: RETURN_SUCCESS The call to GetFirst or GetNext has been successful and no changed postcode or STD Code was found. RETURN_CHANGED_POSTCODE The call to GetFirst or GetNext has been successful and the Postcode has been changed from that supplied in the lookup or search. RETURN_CHANGED_STD The call to GetFirst or GetNext has been successful and the STD Code has been changed from that supplied in the lookup or search. RETURN_CHANGED_POSTCODE_STD The call to GetFirst or GetNext has been successful and the Postcode and STD Code has been changed from that supplied in the lookup or search. RETURN_OUTCODE_ONLY The call to GetFirst or GetNext has been successful but a result at Outcode level (first part of the postcode) only has been returned. RETURN_OUTCODE_BREAK The call to GetFirst or GetNext has been successful but no new result has been returned - this is simply a break to allow the user to cancel a potentially long lookup or search. RETURN_NOT_FOUND No Records were found matching the lookup or search criteria given. RETURN_ERROR_OPENING_FILES Postcode was unable to open it's data files. Please check the software is installed correctly. RETURN_END_OF_SEARCH This indicates that the lookup or search has completed and there are no further records to return - no new record has been returned by this call. RETURN_DATA_LICENCE_ERROR Postcode was not correctly registered. Please run the Welcome program to register. RETURN_MAX_LIMIT_REACHED This indicates that more than the maximum allowable (300) records have been returned from a single lookup or search. This would not happen in the case of a single Postcode lookup and with other lookup's would indicate that the criteria is far too vague to be useful. RETURN_NO_SEARCH_DATA No lookup or search data was specified, i.e. the Lookup property was not set or the Search criteria was all blank. RETURN_INVALID_LOOKUP_TYPE An invalid lookup type was given, i.e. you did not pass the value afdpc.LOOKUP_MODE or afdpc.SEARCH_MODE to the GetFirst function or if you did it was summed with another value that is invalid. API Options - The required options are summed to pass to the GetFirst Function LOOKUP_MODE This indicates a lookup (e.g. for a Postcode or a general lookup string) is being carried out. The Lookup property must be set to do this. SEARCH_MODE This indicates a search (e.g. for a Postcode or a general lookup string) is being carried out. Search criteria must have been set. OUTCODE_BREAKS This indicates that with lookup's other than for a postcode, the GetFirst function will return afdpc.RETURN_OUTCODE_BREAK (5) when it reaches the end of an outcode (first part of the postcode before the space, e.g. B11) for the lookup. This is recommended as it allows the user to cancel in the middle of a lookup and stops your application appearing to have "hung" in the middle of a long lookup. OUTCODE_SKIP This can be specified if you only require the first result per outcode to be returned from your lookup, this is considered advanced functionality and would not be useful to most users.
The following is a complete list of all the properties found in the afdpc class.
Property Type Description Lookup ReadWrite String Specifies the Lookup String to use for a lookup. Postcode ReadWrite String The postcode to search for / the postcode for the returned address. PostcodeType ReadOnly String The type of Postcode for the returned address, i.e. Large user (returns LU) or Small User (returns SU). Organisation ReadWrite String The organisation to search for / the organisation for the returned address. Please note that Postcode does not contain Organisation data so this will only be present if an obvious organisation was included in the lookup or search, e.g. 'AFD Software Ltd, IM8 1RF' Prop ReadWrite String The property to search for / the property for the returned address. Please note that Postcode does not contain Property data so this will only be present if additional non-numeric data has been included in the lookup or search, e.g. 'The Bungalow, B11 1AA'. Street ReadWrite String The street to search for / the street for the returned address. Locality ReadWrite String The locality to search for / the locality for the returned address. Town ReadWrite String The town to search for / the locality for the returned address. County ReadWrite String The County to search for / the county for the returned address. CountyOption ReadWrite String The type of County to use for the search or lookup. Mailsort ReadOnly String The Mailsort Code for the returned address. StdCode ReadWrite String The STD Code to search for / the likely STD Code for the returned address. GridN ReadOnly String The Grid Reference Northing for the returned address - blank for non-Plotter users GridE ReadOnly String The Grid Reference Easting for the returned address - blank for non-Plotter users CensusCode ReadOnly String The Censation Census Code for the returned address. Affluence ReadOnly String The Affluence Description for the returned address. LifeStage ReadOnly String The LifeStage Description for the returned address. AdditionalCenusInfo ReadOnly String Additional Census Information for the returned address.
The following is a complete list of all the functions found in the afdpc class.
Function Parameters Returns Description New - - Default constructor for this class - creates a new instance with all properties initialised to be blank. New String - Creates a new instance of the class from a stored string representation of a previous instance of the class (use ToString function to retrieve this). ErrorText Long String Takes a negative return value of a call to the GetFirst or GetNext function and returns a string representation of the error number. Static in C#. ListStr - String Returns a formatted string representation of the address optimised for displaying in a list box for end-user display. ToString - String Returns a string representation of this class, allowing the instance to easily be saved in minimal memory or on disk for later retrieval of the address. Note this does not save the state of any lookup or search in progress. GetFirst Long Long Takes the type parameter (see API Options under constants for possible values). Providing that the Lookup Property or Search properties (as appropriate) have been set this will return the first result or otherwise an appropriate error number. GetNext - Long This returns subsequent results following a call to GetFirst. This should be called in a loop until the return value RETURN_END_OF_SEARCH or RETURN_MAX_LIMIT_REACHED is returned indicating the end of the search. GetChange String String This is incorporated as part of the GetFirst function so is not normally needed to be called on it's own. However if it is it takes a postcode and will return the new postcode if it has changed. It will return a blank string if there is no change to the supplied postcode or it does not exist.
| Pocket Postcode | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
![]() |
| Manual Contents | ||||||||||||||||||
| ||||||||||||||||||
![]() |
| Appendix | ||||||||||
| ||||||||||
![]() |