31
Introduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis: This is an introductory course intended for graduate students and those who are interested in expanding their options for dealing with data analysis and modeling problems in fisheries science. The course will cover essential programming concepts using the Visual Basic Computer language. Topics & Examples: Using Visual Basic for Applications (VBA). Build your own functions to use in spreadsheets. Working with vectors and arrays. Subroutines and basic models for fisheries stock assessment. Non-linear parameter estimation and likelihood profiling. Monte Carlo methods in spreadsheets. Using Visual Basic to Build Applications: Documenting programs and using pseudocode. Declaring local and global variables, objects. Using consistent naming conventions. Reading and writing data files. Building applications and compiling executables. Building multi-species population models. Debugging Projects. REQUIRMENTS -Preferable a notebook computer with Microsoft Excel (office 97 or newer). -Visual basic 5.0 or 6.0 -Good sense of humor and doughnuts!

Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

  • Upload
    vudien

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Introduction to Visual Basic Programmingwith Special Reference to Fisheries

April 8th-11th

Synopsis:This is an introductory course intended for graduate students and thosewho are interested in expanding their options for dealing with data analysisand modeling problems in fisheries science. The course will cover essentialprogramming concepts using the Visual Basic Computer language.

Topics & Examples:

• Using Visual Basic for Applications (VBA).

• Build your own functions to use in spreadsheets.

• Working with vectors and arrays.

• Subroutines and basic models for fisheries stock assessment.

• Non-linear parameter estimation and likelihood profiling.

• Monte Carlo methods in spreadsheets.

Using Visual Basic to Build Applications:

• Documenting programs and using pseudocode.

• Declaring local and global variables, objects.

• Using consistent naming conventions.

• Reading and writing data files.

• Building applications and compiling executables.

• Building multi-species population models.

• Debugging Projects.

REQUIRMENTS-Preferable a notebook computer with Microsoft Excel (office 97 or newer).-Visual basic 5.0 or 6.0-Good sense of humor and doughnuts!

Page 2: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

1 Course Outline

1.1 Using Visual Basic for Applications (VBA)

-VBA can be used in all Microsoft Applications, including Powerpoint, Word,Access and Excel.

Using Functions in Excel Introduction to functions and function arrays.

Building Functions in Excel Building user defined functions, and whento use function arrays. There are a limited number of functions inExcel, and we can build our own functions to carry specific analysis.

Vectors and Arrays Defining Vectors and Arrays in VBA, and how to usethem efficiently. Looping and conditional statements.

Building Models and Programs in VBA Using Subroutines to organizecode. Declaring global and local variables, passing variables betweensubroutines.

Non-linear Parameter Estimation Fitting models to data, or estimatingmodel parameters given the data.

Parameter Uncertainty Bootstrapping, or re-sampling the data. UsingMonte Carlo methods to construct parameter distributions.

1.2 Using Microsoft Visual Basic

Introduction to the Visual Basic Interface Using the interface efficiently.

Program Design Mapping and using pseudocode.

Naming Conventions Using consistent naming conventions, global and lo-cal variables

External Data Files Create or read external data files for your program.

Debugging Programs Using breaks, the immediate window and watchwindow. Error handling.

ii

Page 3: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Contents

1 Course Outline ii

1.1 Using Visual Basic for Applications (VBA) . . . . . . . . . . . ii1.2 Using Microsoft Visual Basic . . . . . . . . . . . . . . . . . . . ii

2 Using Visual Basic for Applications 22.1 Using Functions in Excel . . . . . . . . . . . . . . . . . . . . . 22.2 Building Functions in Excel . . . . . . . . . . . . . . . . . . . 2

2.2.1 Passing Arguments . . . . . . . . . . . . . . . . . . . . 32.2.2 Example: Random Normal Distribution . . . . . . . . 4

2.3 Vectors and Arrays . . . . . . . . . . . . . . . . . . . . . . . . 52.4 Building Models in VBA . . . . . . . . . . . . . . . . . . . . . 6

2.4.1 Example: Stock Recruitment Model . . . . . . . . . . . 72.5 Non-linear Parameter Estimation . . . . . . . . . . . . . . . . 8

2.5.1 Integration Example: Pella-Tomlinson Model . . . . . . 92.6 Parameter Uncertainty . . . . . . . . . . . . . . . . . . . . . . 12

2.6.1 Bootstrapping Your Data . . . . . . . . . . . . . . . . 132.6.2 Sampling Importance Resampling . . . . . . . . . . . . 162.6.3 Example: Depletion Estimator . . . . . . . . . . . . . . 17

3 Building Applications with Visual Basic 203.1 The Visual Basic Design Interface . . . . . . . . . . . . . . . . 203.2 Designing VB Projects . . . . . . . . . . . . . . . . . . . . . . 223.3 Variable Declaration and Naming Conventions . . . . . . . . . 23

3.3.1 Variable Scope . . . . . . . . . . . . . . . . . . . . . . 253.3.2 Variable Types . . . . . . . . . . . . . . . . . . . . . . 26

3.4 Working with External files . . . . . . . . . . . . . . . . . . . 263.5 Debugging and Error Handling . . . . . . . . . . . . . . . . . 28

1

Page 4: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

2 Using Visual Basic for Applications

Visual Basic for Applications, or VBA, can be used in most Microsoft OfficeProducts, however this manual specifically refers to Microsoft Excel. VBA isa programming environment that allows the user to define their own specificfunctions, or create/record macros for carrying out repeated tasks.

2.1 Using Functions in Excel

In Excel, functions can be used to carry out specific calculations based onvalues specified in the spreadsheets. There are many functions available tothe excel user, for example:=Sum(A1:A10)

will sum all of the numbers in the column A and rows 1 . . . 10. The functionitself is simply a computer program that loops over all the numbers in rows 1through 10 and adds them up, then replaces the contents of the cell with thevalue of the function. The “Range” A1:A10 is referred to as the arguments,or simply the range of numbers we want to sum. The word Sum is the name ofthe function, and we call this function by placing a = sign in front of sum. Theactual computer instructions are stored in a Dynamic Link Library or dll.Note that in Excel or Visual Basic Language, function names and variablesare not case sensitive.

Function “arrays” differ from standard functions in that they may returnnumerous results. For example a frequency distribution returns the numberof observations that are within a bin range. One of the more common arrayfunctions used in Excel is the Table Function. Function arrays can also beprogrammed, but are implemented in the spreadsheet environment by typingin the function name and pressing Ctrl Shift Enter.

2.2 Building Functions in Excel

Creating your own function in excel is fairly strait forward. User definedfunctions are written in the Visual Basic Editor which can be located underthe Tools . . .Macros menu, or simple press Alt-F11.

Functions must be written in a Module. To create a new user definedfunction, under the Insert menu select Module. For a simple demonstra-tion lets create a function that returns a random number between 0 . . . 100.

2

Page 5: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

In the module enter the following code:

Function Junk()Junk = 100 * Rnd()

End Function

If we now return to the spreadsheet view, and select an empty cell, wecan call the new function Junk by entering =Junk() into the cell and pressenter.

To create a function array, we would highlight a range of cells, and in theformula bar type =Junk() and press Ctrl-Shift-Enter. Note that in eachcell the same random number is copied into each cell. If, however, this rangeof cells is copied and pasted elsewhere on the spreadsheet, the function iscalled again and new random numbers are drawn.

2.2.1 Passing Arguments

Much like the =sum() function, we can pass arguments to a function, suchas numbers to add together, divide etc. These arguments can either beOptional or Required. To write a function with required arguments thefunction would start with:

Function Junk(x as Integer)

where x is treated as an Integer in program. If we are passing Optionalarguments the function would start with:

Function Junk(Optional x as Integer)

In this case if x is not specified then the computer assumes the value of x= 0. It is necessary then if no argument is presented that the user writes codethat is robust to the optional argument. For example suppose your functionreturns 10/x, if x = 0 then the function is undefined. Making the code robustmight include a statement such as “If x = 0 Then x = 1”, this will insurea divide by zero error does not occur. The following example illustrates theuse of optional arguments.

3

Page 6: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

2.2.2 Example: Random Normal Distribution

Suppose we want to draw a random number X from a normal distributionwith a mean µ and standard deviation σ. This can be accomplished usingthe Box-Mueller scheme:

X = µ + σ√

−2Ln(V1) cos(2πV2)

where V1 and V2 are uniform random numbers between 0 . . . 1. Notein this equation, if µ = 0 and σ = 1 then X will come from a normaldistribution with a mean = 0 and σ = 1. If σ = 0 X will always = 0,therefore our code must ensure that σ 6= 0.The code to implement this function is as follows:

Function RndNorm(Optional Mean As Double, Optional Std As Double)Const PI As Double = 3.141592654 ’This is π a constantDim V1 As Double, V2 As Double, X as Double

’Draw random number from a normal distribution’with a Mean and Std.If Std = 0 Then Std = 1Do

V1 = Rnd()V2 = Rnd()

Loop Until V1 > 0X = Sqr(-2 * Log(V1)) * Cos(2 * PI * V2)RndNorm = Mean + Std * X

End Function

The function RndNorm returns a number randomly sampled from a dis-tribution with an optionally specified mean and standard deviation. Ifno Mean or Std are specified then the function assumes the Mean of thedistribution = 0 and the standard deviation is 1. Also notice the useof the Do Loop, where uniform random numbers are drawn until V1 isgreater than 0. This is done to avoid the undefined situation Log(0).

4

Page 7: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

2.3 Vectors and Arrays

The easiest way to think of vectors and arrays in computer programs is toimaging a set of postoffice boxes, or a continuous set of columns and/orrows in a spread sheet that store information. By definition a vector is acontainer object containing n elements, or a single column in a spreadsheetof n rows. An 2 dimensional array is a container object containing n vectors,or a block of cells on a spreadsheet containing n columns and m rows. Arrayscan also extend into 3 dimensions, 4 dimensions, . . . etc. The spreadsheetenvironment is limited to 2-dimensions, and its often convenient to work inhigher dimensional space. For now, I will stick with simple 2 dimensionalexamples and leave it to you to extend your thinking into higher dimensionalspace.

To declare a variable or vector within a function or subroutine, or justabout anywhere in the visual basic interface, use the Dim statement, whichimplies Dimension. If there are no arguments such as:Dim a as Single

then the variable a can store a one value with Single precision. If we declare:Dim b(10) as Double

then the variable b stores 11 values with double precision, and each value isindexed with a numbers 0 . . . 10. Alternatively, the index can be modifiedsay to represent years such as:Dim b(1970 to 1980) as Double

To declare an array, we use the same statement, however we add a seconddimension:Dim c(1 to 10, 1 to 10) as Double

This statement creates a 2-dimensional array, or matrix with 10 rows and 10columns. If we want to store the number 2.2718 in the center of the matrixwe can write:c(5, 5)=2.2718

or perhaps extract the diagonal elements from c into vector b using:

For i= 1 to 10b(i)=c(i,i)

Next i

5

Page 8: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

The use of the For Next loop uses an index i as a counter, where the firstrun through the loop i=1, and the second run i=2 . . . , and the loop finisheswhen i=10.

The loop is what makes programming very powerful, where a single lineof code is repeated many many times until some condition is met. Manyinstructions can be contained within a loop statement, including additionalloops.

2.4 Building Models in VBA

When building models of any type, it is often best to separate the codeinto smaller more manageable chunks. A set of instructions for carrying outa complicated statistical analysis, or large catch-at-length model may takeseveral thousand lines of code. Keeping the logic straight in your head is go-ing to be difficult, especially when the same tasks are repeated several times.In Visual Basic, we can use Functions to call other functions, or Subroutinesto call other Subroutines or functions. This is of great advantage, because wecan now separate the code into logical compartments, where specified taskscan be carried out. As an simple example, if we were to write computerprogram that calculated the variance:

σ2 =

(x − x)2

n − 1

we first have to compute the mean x before we can start calculating themean squared error

(x − x)2. In this case we could separate code intotwo functions: one function to return the mean of all x’s, and the second tocalculate and return the variance. Obviously this is a gross simplification,but it makes the point that large jobs can be broken down into a a set ofsmaller jobs, which are carried out in a logical sequence.

One of the most important steps in building computer models, especiallymodels intended for analysis of real data, is to demonstrate for yourself thatthe model actually works correctly. This is equivalent to a mathematicalproof. The easiest way to accomplish this is to use the model to generateobserved data sets that you have, or plan to collect in the future. Then usethe model to analyze the data to see if you can recapture the true parametersused to generate the data in the first place. The following example, generatesa recruitment observation from a Ricker stock recruitment curve given model

6

Page 9: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

parameters, and the standard deviation in recruitment anomalies. The func-tion also calls the previous function example for sampling a random numberfrom a normal distribution.

2.4.1 Example: Stock Recruitment Model

In this example the function Ricker returns a predicted recruitment valuegiven a spawning stock level and model parameters. The function alsocalls a separate function to add some log-normal error to the predictedrecruitment.The model is as follows:

Rt = αSte−βSteεt εt ∼ N(0, σ2

r)

where Rt is the predicted recruitment, St is the observed spawning stocklevel, α is the maximum recruits per spawner (or the slope at the origin ofthe stock recruitment curve), and β determines the recruitment capacity.The error term εt is a random normal variate with a mean of 0 and avariance σ2

r . Use the following function to implement this model:

Function Ricker(a as Double, b as Double, St as double, Optional sig as Double)Dim Rt as Double, wt as Double

If sig > 0 Then wt = RndNorm(0,sig)Rt = a * St * Exp(-b * St)Ricker = Rt *Exp(2*wt)

End Function

To call this function from within the excel spreadsheet, use:=Ricker(4.5,0.001,100). Using these values, the function should re-turn 407.177 recruits. Notice that no error value was used so the deter-ministic value was returned. To generate noise include the optional sigerror term such as: =Ricker(4.5,0.001,100,0.1). This will introducesome error to the deterministic model, where the residuals between de-terministic model and “noisy” model will have a standard deviation σ =0.1.

The example above is a good starting point to building larger, more

7

Page 10: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

complicated models, and you should work through it several times to ensureyou understand the basic concepts before starting into a large project. Also,consider this example as a smaller component of a much larger populationdynamics model. For example you can now use this simple ricker functionas a stock-recruitment function for a ‘delay-difference’ model.

2.5 Non-linear Parameter Estimation

Estimating model parameters requires a statistical fitting criterion that mustbe minimized, such as a sum of least squares, or maximized such as a log-normal likelihood. Minimization or maximization can be carried out bothanalytically (usually for simple problems such as linear regression), or nu-merically. There are many algorithms available for carrying out numericalminimization, one such algorithm is available for use in Excel (Solver). Thisalgorithm, known as the ‘GRG’ or generalized reduced gradient method, isone of the more sophisticated and robust algorithms available. In terms of

finding the global minima, it actually out performed ‘AD Model Builder’ for

estimating parameters in a catch-at-age model. Note that solver can onlyminimize one target cell, not a range of target cells on the spreadsheet, soyour objective function must be located in one cell.

Solver can be called from within the VBA environment, using the state-ment SovlerOK. Additional arguments must be specified, and the easiestway to set it up is to record a macro. Under the Tools menu, selectMarco...Record, then run solver and stop recording. After the recordingsession has finished you can examine the code in the VBA editor. The codeshould look similar to what is shown in Figure 1. In order to call solverwithin your code, you have to included a reference library. This can be donein the VBA editor under the Tools menu. Open the reference library andselect the Solver reference; this will then add a “Reference to SOLVER.xla”.Essentially you have declared a dynamic link library in your project, andfunctions such as SolverOK can now be called within the code. Use shouldrun the macro to ensure that the library is working correctly. If it is workingcorrectly, the solution interface should appear, as it would normally appearsafter you run solver from within excel.

The advantage of being able to call solver from within VBA, is you cannow call solver repetitively to preform repeated task. In the case of fisheriesstock assessments likelihood profiles can be constructed by looping over fixedvalues of one parameter and estimating all other parameters using solver.

8

Page 11: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Sub Macro1() ' ' Macro2 Macro ' Macro recorded 3/29/2002 by Steven Martell ' ' SolverOk SetCell:="$H$25", MaxMinVal:=2 _ , ValueOf:="0", ByChange:="$C$5" SolverSolve Ok End Sub

Figure 1: Example of a recorded macro for a solver run in Excel.

This is called the conditional likelihood profile.There are two basic commands needed to run solver: 1) SolverOK is used

to set up the problem, and 2) SolverSolve which is equivelent to hitting theOK button on the solver setup interface. Note once you have set up solver,you can repeatedly run solver by just using the ‘‘SolverSolve True’’ state-ment. You do not have to repeatedly setup solver, however if you are addingadditional parameters, or changing parameter constraints between functioncalls then you do have to reset the solver options using SolverOk.

2.5.1 Integration Example: Pella-Tomlinson Model

The Pella-Tomlinson model assumes that change in biomass over timecan be described with the following ordinary differential equation:

δB

δt= rB

[

1 −(

B

Bo

)m−1]

− qEB where m > 1

where r is the intrinsic rate of increase, Bo is the unfished stock size, andm determines where the function is maximized. In other words at whatstock biomass is δB

δtmaximized; if m = 2 this occurs at Bo/2. The qEB

9

Page 12: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

term is just the catch. The explicit form of the difference equation canbe written as:

Bt+δ = Bt + rBtδ − rBt

(

Bt

Bo

)m−1

δ − qEtBtδ

We might proceed here and use this equation as our population dynam-ics model, where the model is driven with a time series of fishing efforts(independent variable). We then fit this model to set of observed catchdata, by estimating 4 parameters (Bo, r,m and q). Anyone who has builta couple of logistic population model has noticed that these differenceequations tend to be numerically unstable (e.g. often see populationovershoot carrying capacity then oscillate around it for some time), es-pecially when high r values are assumed. A more numerically stableapproach to this model is to use a semi-implicit form and carry out anumber integration steps t/δ each year:

Bt+δ = Bt + rBtδ − rBt+δ

(

Bt

Bo

)m−1

δ − qEtBt+δδ

then solve for Bt+δ

Bt+δ =Bt(1 + rδ)

1 +[

r(Bt

Bo)m−1 + Ft

]

δ

where Ft = qEt and the predicted catch Ct+δ = FtBt+δ

If we assume errors in catch rates over time are log-normally distributedthen the following log likelihood function can be used as a statisticalfitting criterion:

l(Ct|Bo, r,m, q) =n − 1

2log

n∑

t=1

log

(

Ct

Ct

)2

where Ct are the observed catches. This semi-implicit form become morenumerically stable when annual time steps are broken up into smallertime steps (integrating the logistic dynamics over time). It also providesa good example for using function arrays to implement this model. Inthis case, the data (an effort vector) are passed to the function and thefunction returns a matrix of predicted biomass, fishing mortality ratesand catches.

10

Page 13: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Sub Retrospective() 'Do Retrospective Analysis, Dim retyear As Integer Dim i As Integer, j As Integer retyear = Sheets(1).Range("RetYear") If retyear > 2001 Then MsgBox ("Must set an Earlier Retro Year") Exit Sub End If 'Set up Solver SolverOk SetCell:="$C$15", MaxMinVal:=2, ValueOf:="0", _ ByChange:="$C$4,$C$5,$C$6,$C$7" For i = retyear To 2001 Sheets(1).Range("RetYear") = i SolverSolve True 'Now Save Parameters to a Range j = j + 1 With Sheets(1).Range("results") .Rows(j + 1) = i .Rows(j + 1).Columns(2) = Sheets(1).Range("Bo") .Rows(j + 1).Columns(3) = Sheets(1).Range("rr") .Rows(j + 1).Columns(4) = Sheets(1).Range("m") .Rows(j + 1).Columns(5) = Sheets(1).Range("q") End With Next i End Sub

Figure 2: Example code using solver to do a retrospective analysis.

Once the example above is set up in the spread sheet, we can then com-pute the likelihood, and estimate the 4 model parameters given a set of catchand effort data. In this case use solver to minimize the likelihood functionby changing Bo, r,m and q. This will give the maximum likelihood estimatefor the parameters. One of the more interesting things to do is to carry outa retrospective analysis, where we estimate model parameters with only alimited set of data. For example if you have a time series of data that goesfrom 1950 to 2001, we are certainly interested in seeing how more recentobservations change parameter values. The code shown in Figure 2 can beused to carry out this retrospective analysis.

Note here that we can refer to ‘named’ cells on the spreadsheet using the.Range{‘‘cell Name’’) command. The code shown in Figure 2 proceedsas follows:

11

Page 14: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

1. Read the first year to start the retrospective analysis off the spread-sheet.

2. Set up the solver options, (note we are minimizing the likelihood in cellC:15).

3. Loop over each retrospective year, and calculate the likelihood of thedata up to the retrospective year. For example if the retrospectiveyear is 1960, then only use the data from 1950 to 1960 to estimateparameters. This can be accomplished by using=If(retroyear<Year,Likelihood, 0),which is just a standard IF statement in Excel.

4. Run solver and estimate model parameters.

5. Store the estimated parameter values on the spreadsheet in an areanamed Results

2.6 Parameter Uncertainty

Parameter uncertainty is just another fancy word for the confidence intervalor standard error for the frequentist, or credible interval or marginal distri-bution for the Bayesian. The gist of addressing parameter uncertainty in afisheries context is to report how errors in the data, or assumptions influenceprojected outcomes for different fisheries policy options. Bayesian inference

is a school of statistical theory where probability models are fit to data, and

the results are summarized as probability distributions for model parameters

and predicted states. The school of inference lends itself well to fisheriesmanagement, in that we can parameterize models in ways that provide prob-ability distributions over a range of possible management objectives. Forexample, it is possible to assign a probability of Bt+1 < Bt over a rangesof quota options. When presented correctly, these probability distributionsare extremely useful for fisheries managers who face management decisions.The key word here is “decisions”!. If a stock assessment scientist only pro-vides one number, say the maximum likelihood estimate, there is no decisionrequired; therefore,no management required (wouldn’t that be nice!).

So how do we proceed to construct confidence intervals, or probabilitydistributions in excel. There are some commercial packages available thatmay help you, but most of these are on the order of 1, 000 US or more (e.g.

12

Page 15: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Crystal Ball), and in the time spent learning how to use these addin’s, youcould have programmed as simple routine, such as “Bootstrapping”. In thissection, I will go over some basic elements required for programming yourown “Bootstrapping” routine, and ‘SIR’ or sampling importance resampling.

2.6.1 Bootstrapping Your Data

The bootstrap method is simple a process of generating new data sets byrandomly sampling from the original data set. There are many variationsand approaches to the method, but the simplest form which is most easilyunderstood is to randomly sample the data set with replacement. For exam-ple lets say we have 100 observations of length at age data and we want tofit a standard von Bertalanffy growth model to the data, where the model is:

La = L∞

(

1 − e−k(a−to))

+ ε where ε ∼ N(0, σ2a)

Because the error terms ε are normally distributed the correspondingprobability model is the normal distribution. We then proceed to estimatemodel parameters by minimizing the negative log-likelihood1 (or the negativelog of the normal distribution). We now have maximum likelihood estimatesof model parameters. But lets say we want to compare these estimates withanother population, and determine if they are statistically different. To dothis we have to compare the distribution of one parameter set to the otherparameter set. These distributions can be constructed using bootstrapping.

If you think carefully about the sampling process that was involved incollecting your data in the first place, you were randomly sampling from thepopulation (or at least you think you were). So, given this random sampleyou collected, you’ve estimated a set of parameters Θ. If you were to divideyour sample in half and estimate parameters Θ1 and Θ2 you would nowhave two estimates, but in this case less credibility because of smaller samplesizes. In order to conserve the credibility that comes from sample size, itis better to n random samples from the data set Nboot times, and samplewith replacement. In this case, some of the pairs of data points in n will besampled more than once, and others might be omitted. If you original samplewas representative of the true population, then your bootstrap sample should

1We use log-likelihoods because they are additive, whereas the multiplicative modelcan get you into trouble with you computers floating point because multiplying smallprobabilities together results in tiny weeny numbers. This is especially true if you havelots of data.

13

Page 16: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

also be representative of the true population. For each bootstrap sample, youcan now proceed to estimate model parameters Θi and save these parameterestimates to a file. This set of parameters will have a distribution, with amean and a variance, where the mean is:

θ =1

Nboot

Nboot∑

i=1

θi

and a variance:

σ2θ =

1

(Nboot − 1)

Nboot∑

i=1

(

θi − θ)2

An example of implementing the bootstrap method in VBA is shown inFigure 3. Note that the algorithm first stores the original data in memory,and resets it after the all of the bootstrap samples are done. This is ‘badpractice’. Ask yourself what happens to the original data when the programcrashes (and it will). It’s gone, lost in the computers memory, and be verydifficult to retrieve once you realized the mistake. A better practice would beto set up the spreadsheet such that the bootstrap samples are place elsewhereand the original data is never modified.

14

Page 17: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Sub BooT() Dim Nobs As Long, nSamples As Long, i As Long, j As Long Dim Data() As Double, sdata() As Double Dim ix As Integer WrkSheet = "Boot" '1)Read Observed Data off spread sheet. Nobs = Sheets(WrkSheet).Range("Nobs") nSamples = Sheets(WrkSheet).Range("nSamples") ReDim Data(1 To 2, 1 To Nobs) 'Column1 = age, column 2 = length ReDim sdata(1 To 2, 1 To Nobs) 'Sample data for bootstraps For i = 1 To Nobs Data(1, i) = Sheets(WrkSheet).Range("Age").Rows(i) Data(2, i) = Sheets(WrkSheet).Range("Length").Rows(i) Next i 'Set up Solver SolverOk SetCell:="$C$12", MaxMinVal:=2, ValueOf:="0", ByChange:= _ "$C$5,$C$6,$C$7,$C$8" For j = 1 To nSamples '2)Resample the data with replacement. For i = 1 To Nobs ix = 1 + Rnd() * (Nobs - 1) 'Pick a random number of index 'Now print new sample on spreadsheet. Sheets(WrkSheet).Range("Age").Rows(i) = Data(1, ix) Sheets(WrkSheet).Range("Length").Rows(i) = Data(2, ix) Next i '3)Estimate parameters using solver SolverSolve True '4)Print Parameter sample to results area With Sheets(WrkSheet) .Range("Sample").Rows(1 + j) = j .Range("Sample").Rows(1 + j).Columns(2) = .Range("Linf") .Range("Sample").Rows(1 + j).Columns(3) = .Range("k") .Range("Sample").Rows(1 + j).Columns(4) = .Range("to") .Range("Sample").Rows(1 + j).Columns(5) = .Range("CV") End With Next j '6)Restore original data For i = 1 To Nobs 'Now print original data on spreadsheet. Sheets(WrkSheet).Range("Age").Rows(i) = Data(1, i) Sheets(WrkSheet).Range("Length").Rows(i) = Data(2, i) Next i 'Run Solver again to restore original Parameters SolverSolve True End Sub

Figure 3: Example code for a bootstrap method, where 4 model parame-ter are estimated using solver for each bootstrap sample, then printed tothe spreadsheet where means, variances, and parameter correlations can becalculated.

15

Page 18: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

2.6.2 Sampling Importance Resampling

Sampling Importance Resampling (or SIR) is one of various methods usedfor computing the posterior distribution, usually noted as:

P (Θ|Data)

Where Θ consists of the model parameters. The statement is read as“The probability of the parameters given the data”. The SIR method isreally straight forward, and can be used to approximate high dimensionalproblems. The algorithm proceeds as follows:

1. Set prior bounds (reasonable ranges) for each parameter in the model.Note these can be initial guesses at first, then modified later.

2. Randomly select a set of parameters from these prior distributions.Note that the prior distributions can take any shape including a uniformprior, or a informative prior such as a normal distribution.

3. Compute the probability of the randomly selected parameter set.

4. If the probability satisfies the importance criterion then store the pa-rameters and probability to a file.

5. Repeat steps 2-4 many many (perhaps even millions of times).

By reading this pseudocode, you should be asking yourself what is animportance criterion, prior bounds? These are the key elements required tocarry out SIR. The prior bounds, are simply prior distributions we place onparameter values, or the range which we omit the distribution can come from.For example suppose we were trying to estimate a survival rate, we know thatthis parameter must lie between 0 and 1. But even this prior distribution iscrazy, if S = 0, then there shouldn’t be a population to begin with, and if S =1 we should all be up to our armpits in whatever it is we are studying. Similarrationalizations can be made for population biomass;setting the minimumbound = to the total amount caught, and the upper bound being say, 100Xwhat we caught.

The importance criterion is simply the probability of the parameter valuesrelative to the highest probability. In the following example I use a cumula-tive distribution to calculate the relative importance of the parameter value.

16

Page 19: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Parameter 1P

aram

eter

2

True Parameter Distribution

Parameter 1

Par

amet

er 2

Prior Distribution Sampling Space

Figure 4: Example of sampling uniform prior distributions to construct ajoint posterior distribution for two model parameters.

The idea is shown in Figure 4, where we uniformly sample from a prior dis-tribution, and the information criterion allows us to keep samples that comefrom the true distribution (as shown in the right hand panel).

2.6.3 Example: Depletion Estimator

The Leslie depletion estimator for estimating population abundance as-sumes a closed population and that catch over successive sampling inter-vals can be described as:

C1 = qN1

Ct = q(Nt−1 − Kt−1)

The two model parameters to estimate are q, the fraction of the popu-lation removed per fixed sampling unit, and N1, the initial populationabundance before sampling. The cumulative catch Kt−1, is the indepen-dent variable and the model predicts catch Ct at each sampling interval t.

17

Page 20: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

So given a catch time series one can estimate the initial abundance by fit-ting the regression line Ct = qN1 − qKt−1 to the catch versus cumulativecatch plot.The negative log-likelihood assumes normal error:

L(Ct|N1, q) =(n − 1)

2Log

n∑

t=1

(Ct − obsCt)2

The posterior distribution can be approximated by:

P (N1, q|Ct) ∝ e−L(Ct|N1,q)

To sample from the posterior distribution, carry out the following steps:

1. First draw parameter values from a prior distribution p(lb . . . ub)

2. Evaluate P (p|Ct) and add to Pcum

3. If P (p|Ct) > Pbest then set Pbest = P (p|Ct)

4. If Pcum > Pbest then append the parameter values p to a file andsubtract Pcum from Pbest, and add one to the sample counter i

5. Loop over items 1 . . . 4 until the sample counter i reaches the desirednumber of samples (say 10,000).

6. Finally use the saved parameter estimates to calculate the mean,variance etc. or construct distributions as shown in Figure 5.

In the depletion example, the information criterion continues to be up-dated as the algorithm proceeds. Once a more likely parameter combinationis found, the Pbest is updated. Essentially we are randomly walking aroundin parameter space, such as shown in Figure 4 in the left column, and search-ing for the best possible parameter combination by chance alone. As moreiterations proceed, the Pbest increases in value, so it will take more iterationsto accumulate the necessary probability that is required to save a parametersample. If we want to keep all parameters that are within the 90% confidenceinterval, we might choose to set Pcum to 0.9Pbest. This will also decrease thenumber of iterations required to take n samples.

18

Page 21: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

0.15 0.20 0.25 0.30

500

550

600

650

700

750

800

q

No

0.15 0.20 0.25 0.30

0 5

10

15

0.000 0.002 0.004 0.006 0.008

Figure 5: Example of a joint posterior distribution for a depletion estimator.The histograms represent the marginal distributions for each parameter.

19

Page 22: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

3 Building Applications with Visual Basic

There are essentially 3 basic phases of building a computer application: 1)The Design phase, which is analogous to an architect designing a buildingbefore it is built. 2) The programming phase, where sets of instructions inthe form of functions and subroutines are written to carry out the eventsof the application. 3) The final step, which actually never ends, is the de-bugging phase. The last two phases are an iterative procedure, where theprogrammer should be continuously evaluating potential errors that mightarise, and writing code to handle obvious errors. All programs have bugs,but good programs have fewer bugs.

In this section, I will introduce the basic layout of the Visual Basic (VB)design interface, and how to use various options for designing programs,writing computer instructions (or code), and finally debugging programs.

3.1 The Visual Basic Design Interface

There are 5 basic windows in the VB interface that are of interest: The form

view, the form code,the tool bar,the project explorer and finally the properties

window (see Figure 6).The form is where you can actually design the user interface of you ap-

plications. A user interface allows the user to dynamically interact with thecomputer program, such as pressing a Command Button. There are manytypes of controls that can be placed on the form, and this is usually done inthe design phase of your program.

Computer instructions, or code, can be written in 3 different types of en-vironments. The three types are the Form environment, the Module environ-ment and the Class module environment. The Class module is usually usedin “Object Oriented Programming”, where user defined objects are definedand created. We will not cover the use of Class Modules here. Much of yourcomputer code will be written in the Form and Module environments. Thefunctional difference between these two environments is their scope.2 Publicvariables declared in a Module’s declaration section can be used throughoutthe project. You cannot declare public variables in a Form environment.

2Variables and user defined functions/subroutines can be declared as local variables(where they are only available within that function, subroutine, or environment), or Publicwhere their scope is global and can be used anywhere in the program.

20

Page 23: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Form: Place controls on form, double click to attach code.

Write Your code here.

Project Explorer, Navigate through forms, modules and class modules.

Properties Window: Set Object Properties Controls you can

drag onto the form.

Figure 6: Example of the Visual Basic design interface.

21

Page 24: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

It is possible to place all of the code for your project in one single formor module environment, however, as the project gets large it is difficult tonavigate through the code so its often desirable to organize the code, muchlike the chapters of a book. In this case, the chapters correspond to keyprogram functions. For example lets say you build a project that reads aset of data, where the data must be manipulated in some fashion before itcan be used in the second component of your project, statistical analysis say.Here the code can be organized such that one module might carry out theinstructions for reading-manipulating the data and placing it into memory. Asecond module can be used to write code for doing the statistical analysis (likeanova). A nice feature here, is that you have now created an independent setof code for doing anova’s that can also be used in future applications. Thisis what is referred to as a code library.

The tool bar is used for placing controls on your form, such as commandbuttons or picture boxes, tabbing environments, text boxes, labels . . . andmillions of other types of controls. The controls I just listed are the mostcommon controls you see in computer programs. Controls elicit user events,such as a command button with a caption that says “Press Button”. Yourjob as a programmer is to attach code to these user events. There is anothergeneral type of event that you should be aware of, the “Load” type events.For example when a program is launched it loads a form, or maybe a splashscreen informing the user about the ‘‘TIP OF THE DAY’. When a form isloaded or closed, these are also considered events and code can be attachedto these events. For example when closing a program, you could ask the userif they want to save changes.

The properties window allows you to add or modify a controls property.Properties include, colors, size, redraw, font, startup position, height, width,. . . etc. Different controls also have different properties. These propertiescan also be modified as the program runs, where you change the propertiesin the code. For example the code shown in Figure 6 randomly changes thebackground color when the user presses the command button.

3.2 Designing VB Projects

Designing a program requires extensive use of simple logic. The key thingto remember here is that all of the code you write in Visual Basic is carriedout one line at a time, and in the order it was called. So the simple logicto always keep in the back of your mind, or better yet, map in simple flow

22

Page 25: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

diagrams (see Fig. 7), are the events, and the order in which events occur.You should actually map out these event driven commands, at least on paper,as they will help you sort out problems in your code, and perhaps even saveyou time in the long run.

Another advantage of mapping out user events prior to actually program-ming your project, is reducing the amount of redundant code. Suppose youhave two separate picture boxes in which your plotting time series data. Youcan save yourself the hassle of writing two separate subroutines for each pic-ture box. Additionally you can organize the layout of each subroutine usingpseudocode format. Pseudocode can be thought of as simplified instructions,or sentences, that describe the set of instructions in your code.

There are no general formatting rules for Pseudocode, but your pseu-docode should contain the logical steps that result in the desired outcome.For example if we want to write a function to calculates the mean of a vector(x = 1

N

i xi), then your pseudocode might look something like:

1. Get number of elements in vector (N).

2. loop over each element in the vector and sum all the components(sum = sum + xi).

3. divide the sum by the number of elements. (mean = sum/N)

You should practice looking at equations and writing pseudocode for thoseequations. Later you can challenge yourself by trying to program these equa-tions, but be sure to check the results with known values. Also watch forstate dependencies, such as population dynamics equations, where one stateis a function of the previous state. These types of equations generally callfor an initialization section (where you must define the first state). Such anexample might be:

Bt+1 = SBt + Rt − Ct

Notice that biomass next year (Bt+1) depends on biomass, recruitment, andcatch in the current year. In the first time step, you must specify an initialbiomass value.

3.3 Variable Declaration and Naming Conventions

Variables can be declared in the Declarations Section, which is alwaysthe top of each environment, within subroutines and functions. I will get to

23

Page 26: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

Run Model

Read_Data Run_Dynamics Save_Output

Run Model Code Read_Catch_file Read_Effort_file

Initial_Calcs For t = 1 to years PopDynamics Recruitment FishingMortality DrawGraphs Next t

CatchGraph BiomassGraph

SaveBiomass SavePredictions

User Event (press command

button)

Event Code (in Form

Enviroment)

Module Code (Collection of functions

and subroutines with global scope)

Figure 7: Example of a code map. Computer code is initialized by events,so structure the flow diagrams such that the user event is the highest level.In this example when the user presses the command button, the commandbutton click event carries out three instructions: (1) read in two data files,(2) Run the population dynamics model, which calls a graphics subroutineat each time step, and (3) save two output files when your done.

24

Page 27: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

the scope of the variable in just one second, but the first thing you shouldalways put in the declarations section is Option Explicit. This forces VBto break on all undeclared variables. Why do this? For the same simplereason we have ‘sepll ’ checkers in our word processors. Your bound to maketyping errors, and if you incorrectly type a variable, Visual Basic will assumeyou are just creating a new variable (of the variant type), and assign a zerovalue to it. This can cause real headaches, especially in large projects. Youcan force variable declaration under the Tools . . . Options . . . Environmenttab as well. I recommend doing this, ALWAYS!

3.3.1 Variable Scope

As mentioned earlier, you can declare local variable and global variables.An easier way to think about variable scope, whether they are single values,vectors, or matrixes, is imagine when in your code they are “created” and“destroyed”. Public or Global variables are ‘created’ when the program islaunched, and ‘destroyed’ when the program is terminated. Local variablesdeclared within a subroutine are created when the subroutine is called, anddestroyed when upon exiting the subroutine. So if you plan to update, ormodify a variables content in multiple subroutines, then the variable mustbe defined outside the subroutine. 3

Variables are can be declared with the following statements:Dim -usually used for local declarations within subroutines or in the dec-larations section of a form or module environment. In latter case, variablescan be modified within all subroutines in the environment.Public -declared in the declarations section at the top of modules or formcode. Can be referred to throughout the entire program.Global -same as public, but can also be modified by other applications.Const -Can be declared anywhere, and cannot be modified. Usually usedfor fixed constants such as π.Static -Usually used for local declarations and differs from Dim, in that thevalue is created once the statement has been called and is destroyed uponexiting the application.

If you declare a static variable within a function, or subroutine, thatvalue of that variable will remain unchanged when calling the function or

3There is an exception to this rule, where variables are passed between subroutines asarguments.

25

Page 28: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

subroutine a second time. A good example here is a counter:

Function Junk() Dim widget as StaticDim i as integerFor i = 1 to 5

widget = widget + 1Next iEnd Function

If this function is called once, then the value of widget =5, if it is calleda second time then the value of widget = 10 . . . etc. If, however, widget wasdeclared using the Dim Statement, then widget is always equal to 5 after anynumber of function calls.

3.3.2 Variable Types

There are a number of variable types that can be declared. A variable typeessentially describes how much memory should be allocated for that variable.For example and integer occupies 2 bytes, while a double precision variableoccupies 8 bytes. The following is a summary of variable types:Integer Range: -32,768 to 32,767Long Range: -2,147,483,648 to 2,147,483,647Single Range: ∼-3.4E-45 to ∼ 3.4E45Double Range: BigString Use this variable type for ‘strings’ or ’text’Variant, can be anything, you should avoid using variants.

When passing variable between subroutines they must be of the sametype. Also, you can convert variable types using statements like CInt(x) toconvert a number to integer type, or CDbl(x) to double, CSng(x) to a single,CLng(x) to a long, CStr(x) to a string.

3.4 Working with External files

In many cases is it often more convenient to read data from a file into thecomputers memory, rather that force the user to enter it every time theapplication is run. Similarly, being able to save the results to a file, andre-run the application with the same results or parameters. Imaging not

26

Page 29: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

being able to save the text you entered in your word processor, this wouldmake writing a thesis much more difficult, in fact, you might as well use atype-writer. Hence it is essential when programming applications, to allowthe user to read and write files from within you program.

Two important command lines are required for reading and writing toexternal files:Open file for Output as file#

Open file for Input as file#

Both of these statements require two things, a file: path and name (e.g.‘‘C:/My Documents/Junk.txt’’). This must be the actual directory andfile name, or a string variable with the appropriate directory and file namespecified. The second thing is the file#, which is just a numeric number ofinteger type. This feature allows you to open more than one file at a timeand read/write objects to different files. Note, you also have to close the filewhen you are finished reading/writing. This can be done using Close file#.The following example is some VB code for opening a bathymetry file andreading in the latitude, longitude and depth information.

Open batfile For Input As 1j = 1Do Until EOF(1)

i = i + 1Input #1, lon, lat, depth(i, j)If i = 181 Then

i = 0: j=j+1End If

LoopClose 1

The input statement can used to read tab delimited numbers in a file,note that the batfile will have 3 columns and many rows. To write datato a file you can use Print, or Write statements which output data in spacedelimited and comma delimited, respectively. Finally the variables that storethe input values should be of the same type (i.e. use string variables forcharacter inputs, and long for integer values greater than 32,767, etc.).

27

Page 30: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

3.5 Debugging and Error Handling

As you develop your application, your code will inevitably contain ‘Bugs’.What is a ‘Bug’ anyways? A bug, is simply when the program does somethingit was not intended to do, or an illogical statement that cannot be defined.There are two general types of bugs, and I like to think of them as annoying“mosquito type” bugs (they won’t hurt you but you spend the next few daysscratching), and “tsetse fly type” bugs (i.e. when the bite, your program willgo to sleep, aka ‘crash’). The mosquito type bugs are simple little thingsthat arise once in a while usually because of sequences of events that youhave not explored. These types of bugs can be really annoying for users, andusually take years to get rid of them all.4

There are some tricks for dealing with the tsetse fly type bugs, and it allstarts with good programming practice, and awareness. For example, whenever you write an equation that contains a division, ask yourself . . . “Is ispossible I might divide by zero here?” If so, you should consider an errorhandler, which may consist of a simple If statement, or jump out of thecode all together and inform the user that the task could not be carried outbecause a ‘div/0’ was about to take place. Excel does this nicely.

The process of eliminating bugs from your program is call “Debugging”,and it essentially involves 3 steps: 1) identifying bugs (users who paid Big$ for your application are really good at this), 2)identifying the events thatled to this bug, and 3) reprogramming the logic to eliminate the bug, or asa last resort, use error handlers to kill the bug.

Identifying bugs is easy, and is usually done while building your applica-tion, but best if you have an independent user play with the program andhave him notify you of events that cause the program to crash. The moredifficult part is tracing the bug. To do this the IDE has interfaces that allowuse to look at variables, break the code at certain points, and step throughthe code line by line so we can watch how variables change over time. Thetwo most commonly used tools are the immediate window, and the watchwindow. When the code is broken during the middle of a function or sub-routine, we can type the variable name ,or equations, or loops etc. in theimmediate window to see its value(s). The watch window is useful for lookingat elements of vectors or arrays, and variable dimensions. Another very use-ful statement is the Debug.Print statement. You can insert this statement

4How many of you get your y-labels cut off when you copy a graph from excel to word!This is an annoying mosquito type bug.

28

Page 31: Introduction to Visual Basic Programming with …everobotics.org/pdf/VBoutline.pdfIntroduction to Visual Basic Programming with Special Reference to Fisheries April 8th-11th Synopsis:

anywhere in the code followed by variable name(s) (separated by comma’s ifmore than 1), and while the program is running it will print the value of thevariables in the immediate window.

To be honest, the debugging process is never done!. As you further de-velop your application you will certainly introduce more and more bugs.Good Luck!

29