Object and Sample handling.

For tips, questions and general information about writing Add-ins, how to package them, and how to submit them to EViews for publication.

Moderators: EViews Gareth, EViews Moderator

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11283
Joined: Tue Sep 16, 2008 5:38 pm

Object and Sample handling.

Postby EViews Gareth » Thu Apr 15, 2010 3:12 pm

A good EViews Add-in should do nothing to harm/destroy/modify the end-user's workfile, other than by producing the desired output. However this can be tricky to achieve without following some guidelines:

Use @getnextname
The easiest way you can harm an end-user's workfile is by over-writing objects. For example, suppose you create a temporary series as part of your add-in:

Code: Select all

series temp = @trend^2 + 1
series out = temp*x

With this code, you have over-written any data the user might have had already stored in a series called TEMP, or OUT. Alternatively, your add-in might fail if they had a non-series object already called TEMP, or OUT.

There are a few ways around this problem. You could use @isobject to test whether there is an object, and if there is call your series something else (although you'd have to test that name too...). You could also use a local subroutine (see below). The easiest method, though, is to use @getnextname to choose your object names for you:

Code: Select all

%tempname = @getnextname("TEMP")
%outname = @getnextname("OUT")
series {%tempname} = @trend^2 + 1
series {%outname} = {%tempname}*x


There is no doubt that this makes your code a lot more difficult to write and follow, but it does avoid the problem of stepping on existing object names.

Tip: Occasionally I find it easier to write my Add-in using proper object names, then once finished, I do a search-and-replace on the object name, thus replacing all TEMPS with {%tempname} in my program.


Samples
You can cache the current workfile sample at the start of your add-in program, and then set the sample back to that sample at the end of your add-in with a simple set of commands:

Code: Select all

%smpl = @pagesmpl   'store workfile sample in a string variable
'add your code here
smpl {%smpl}     'set workfile sample back to that stored in your string variable



Deletion of temporary/intermediary objects
You should be careful to delete any temporary objects you've created at the end of your Add-in, so that the workfile doesn't become cluttered with these unnecessary objects. I will often use a !debug variable to control this deletion, so that if the add-in is in "debug" mode, the objects aren't deleted, which makes debugging the add-in easier.

Note if your add-in has error handling, where by you stop the add-in early if something goes wrong (or if the user hits cancel on a dialog), you should delete any objects you have created up to that point.


Use of subroutines
Using subroutines inside your add-in can help by-pass many of the issues mentioned above. For example if your add-in is inside a subroutine, you can use the

Code: Select all

local smpl

command to tell EViews to keep the workfile sample unchanged at the end of your add-in, without you having to do it manually.

More importantly, you can use a local subroutine to handle the over-writing of existing objects, and the clean-up/deletion of temporary objects. Local subroutines will do both automatically - any existing objects are never over-written in a local subroutine, and all objects created inside a local subroutine are automatically deleted at the end of the subroutine.

However local subroutines cannot access any objects that are in the workfile, other than those that are passed into them as part of the subroutine definition. Further, even if an object was passed into the subroutine, not all of its views/procs may be available since they rely on objects that were not passed into the routine. This means that the occasions where using local subroutines is viable are extremely limited.
Follow us on Twitter @IHSEViews

Return to “Add-in Writing area”

Who is online

Users browsing this forum: No registered users and 1 guest