How to resolve the algorithm Define a primitive data type step by step in the Visual FoxPro programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Define a primitive data type step by step in the Visual FoxPro programming language

Table of Contents

Problem Statement

Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Define a primitive data type step by step in the Visual FoxPro programming language

Source code in the visual programming language

LOCAL o As BoundedInt
o = NEWOBJECT("BoundedInt")
DO WHILE NOT o.lHasError
    o.nValue = o.nValue + 2 && will get as far as 9.
    ? o.nValue
ENDDO

DEFINE CLASS BoundedInt As Custom
nValue = 1	&& default value
lHasError = .F.

PROCEDURE nValue_Assign(tnValue)
*!* This method will check the parameter and if
*!* it is out of bounds, the value will remain unchanged 
*!* and an error generated.
tnValue = CAST(tnValue As I)
IF BETWEEN(tnValue, 1, 10)
    THIS.nValue = tnValue
ELSE
    ERROR "Value must be between 1 and 10."
ENDIF
ENDPROC

PROCEDURE Error(nError, cMethod, nLine)
IF nError = 1098
    MESSAGEBOX(MESSAGE(), 0, "Error")
ELSE
    DODEFAULT()
ENDIF 		
THIS.lHasError = .T.
ENDDEFINE


  

You may also check:How to resolve the algorithm Associative array/Creation step by step in the Vala programming language
You may also check:How to resolve the algorithm Here document step by step in the Arturo programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Lua programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Dart programming language
You may also check:How to resolve the algorithm First-class functions step by step in the Lingo programming language