Unknown type "zcalc_result" - eclipse

I am trying to run a unity test on an abap code using Eclipse.
I am learning from a video series on youtube from a channel called Abap101.
Everything was going well until he uses a variable from the zcalc_result type.
It seems to run well on the video, but my code displays the error message "ZCALC_RESULT TYPE IS UNKNOWN"
REPORT zfsaf_tdd3_babysteps.
*Desenvolvedores:
*Salário igual ou aima de $3000, desconto de 20%
*Salário abaixo de $3000, desconto de 10%
*
*DA e Testadores:
*Salário igual ou acima de de $2500, desconto de 25%
*Salário de $2500, desconto de 15%
CLASS employee DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING employee_name TYPE string
employee_salary TYPE zcalc_result
employee_position TYPE string.
METHODS get_salary RETURNING VALUE(salary) TYPE zcalc_result.
METHODS get_position RETURNING VALUE(position) TYPE string.
PRIVATE SECTION.
DATA name TYPE string.
DATA salary TYPE zcalc_result.
DATA position TYPE string.
ENDCLASS.
CLASS employee IMPLEMENTATION.
METHOD constructor.
me->name = emplyee_name.
me->position = employee_position.
me->salary = employee_salary.
ENDMETHOD.
METHOD get_salary.
salary = me->salary.
ENDMETHOD.
METHOD get_position.
position = me->position.
ENDMETHOD.
ENDCLASS.
CLASS test_payment_calculator DEFINITION FOR TESTING RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA test_employee TYPE REF TO employee.
DATA test_payment_calculator TYPE REF TO payment_calculator.
METHODS setup.
METHODS calc_developer_bellow_limit FOR TESTING.
ENDCLASS.
CLASS test_payment_calculator IMPLEMENTATION.
METHOD setup.
CREATE OBJECT me->test_payment_calculator.
ENDMETHOD.
METHOD calc_developer_bellow_limit.
CREATE OBJECT me->test_employee
EXPORTING
employee_name = 'Chaves'
employee_position = 'Developer'
employee_salary = '1500.00'.
DATA(value) = me->test_payment_calculator->calculate_payment( ).
cl_abap_unit_assert=>assert_equals( exp = '1350' act = value ).
ENDMETHOD.
ENDCLASS.
CLASS payment_calculator DEFINITION.
PUBLIC SECTION.
METHODS calculate_payment IMPORTING emploee TYPE REF TO employee
RETURNING VALUE(value) TYPE zcalc_result.
ENDCLASS.
CLASS payment_calculator IMPLEMENTATION.
METHOD calculate_payment.
value = '1350.00'.
ENDMETHOD.
ENDCLASS.
I was expecting to run a test activate the code, but I get the given messsage when a I try to activate it.

It appears that zcalc_result is a custom data-type which still needs to be defined. It could be defined as a type within the program using the TYPES keyword, or as a global type in the data dictionary (transaction SE11). The name starting with the letter z hints at the latter, but it could just as well be the former.
When you are following a tutorial, then it is possible that the author already did that but you skipped that part, that the author is going to do that next, or that the author forgot to mention it.
Judging from context, the intended purpose of this type is to hold a currency value. So if you want to declare it yourself, you would do that as a packed number (type P) with a reasonable length and two decimals. For example with this line at the top of the program:
TYPES: zcalc_result TYPE p LENGTH 10 DECIMALS 2.

Related

Trouble understanding private attributes in classes and the class property method in Python 3

This class example was taken from here.
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
temperature = property(get_temperature, set_temperature)
The idea here is that when we create an instance of Celsius and set the temperature attribute (e.g. foo = Celsus (-1000) ), we want to make sure that the attribute is not less than -273 BEFORE setting the temperature attribute.
I don't understand how it seems to bypass self.temperature = temperature and go straight to the last line. It seems to me like there are three attributes/properties created here: the Class attribute, temperature; the Instance attribute, temperature; and the set_temperature function which sets the attribute _temperature.
What I DO understand is that the last line (the assignment statement) must run the code property(get_temperature, set_temperature) which runs the functions get_temperature and set_temperature and intern sets the private attribute/property _temperature.
Moreover, if I run: foo = Celsius(100) and then foo.temperature, how is the result of foo.temperature coming from temperature = property(get_temperature, set_temperature) and thus _temperature AND NOT self.temperature = temperature? Why even have self.temperature = temperature if temperature = property(get_temperature, set_temperature) gets ran every time the foo.temperature call is made?
More questions...
Why do we have two attributes with the same name (e.g. temperature) and how does the code know to retrieve the value of _temperature when foo.temperature is called?
Why do we need private attributes/properties an not just temperature?
How does set_temperature(self, value) obtain the attribute for parameter value (e.g. the argument that replaces value)?
In short, please explain this to me like a three year old since I have only been programming a few months. Thank you in advance!
When we are first taught about classes/objects/attributes we are often told something like this:
When you look up an attribute like x.foo it first looks to see if
'foo' is an instance variable and returns it, if not it checks if
'foo' is defined in the class of x and returns that, otherwise an
AttributeError is raised.
This describes what happens most of the time but does not leave room for descriptors. So if you currently think the above is all there is about attribute lookup property and other descriptors will seem like an exception to these rules.
A descriptor basically defines what to do when looking up / setting an attribute of some instance, property is an implementation that lets you define your own functions to call when getting / setting / deleting an attribute.
When you do temperature = property(get_temperature, set_temperature) you are specifying that when x.temperature is retrieved it should call x.get_temperature() and the return value of that call will be what x.temperature evaluates to.
by specifying set_temperature as the setter of the property it states that when ever x.temperature is assigned to something it should call set_temperature with the value assigned as an argument.
I'd recommend you try stepping through your code in pythontutor, it will show you exactly when get_temerature and set_temperature are called after which statements.

Why does VB disallow type conversion from parent to subclass?

I have already asked a question here where I basically require an instance of a base class to be converted into a subclass (or a new instance of the subclass to be created using the instance of the base class' properties). The conclusion seems to be that the best way to do this is to manually assign every property I need to transfer in the constructor of the base class.
While this is feasible in some cases, it certainly is not when there are many properties to transfer, or when the base class is subject to change — every time you add a property to the base class, the constructor needs to be changed too, so this solutions is inelegant.
I have searched online, and can't see any reason for why this kind of type-casting isn't implemented. The arguments I have seen so far describe this operation to 'not make any sense' (making a minivan from a car was an analogy I saw), question what to do about the non-inherited variables in the subclass, or claim that there must be some better solution for what was trying to be achieved.
As far as I can see, the operation doesn't need to 'make sense' as long as it's useful, so that isn't much of a good reason. What's wrong with adding a few more properties (and perhaps methods/overriding them) to change an instance into a subclass? In the case of the non-inherited variables, that can simply be solved by allowing this kind of type-cast only a constructor is added to the subclass or by just simply setting them to their default values. After all, constructors usually call MyBase.New(...) anyway. What's the difference between using the constructor of the base (essentially creating a new instance of the base) and using an instance which is already initialised? Lastly, I don't think the third argument is well-justified — there are times when all of the other solutions are inelegant.
So finally, is there any other reason for why this kind of casting isn't allowed, and is there an elegant way to circumvent this?
Edit:
Since I don't know a lot about this topic, I think I meant to say 'convert' rather than 'cast'. I'll also add an example to show what I'm trying to succeed. The conversion would only be allowed at the initialisation of the Subclass:
Class BaseClass
Dim x as Integer
Dim y as Integer
End Class
Class Subclass1 : Inherits BaseClass
Dim z as Integer
Sub New(Byval value As Integer)
'Standard initialisation method
MyBase.New()
z = value
End Sub
Sub New(Byval value As Integer, Byval baseInstance As BaseClass)
'Type conversion from base class to subclass
baseInstance.passAllproperties()
'This assigns all properties of baseInstance belonging to BaseClass to Me.
'Properties not in BaseClass (eg. if baseInstance is Subclass2) are ignored.
z = value
End Sub
End Class
Class Subclass2 : Inherits BaseClass
Dim v As Integer
End Class
What you describe is not casting. Have you ever heard the expression"to cast something in a different light"? It means to look at the same thing in a different way or to make the same thing look different. That is the exact way that the term "cast" is used in programming. When you cast, you do NOT change the type of the object but only the type of the reference used to access the object. If you want to cast from a base type to a derived type then the object you're referring to has to actually be that derived type. If it's not then you're not performing a cast but rather a conversion.
So, why can't you convert an instance of a base type to an instance of a derived type. Well, why would you be able to? Yes, it's something that might save writing a bit of code on occasion but does it actually make sense? Let's say that you have a base type with one property and a derived type that adds another property. Let's also say that that derived type has constructors that require you to provide a value for that second property. You're suggesting that the language should provide you with a way to magically convert an instance of the base class into an instance of the derived class, which would mean it would have to slow you to circumvent that rule defined by the author via the constructors. Why would that be a good thing?
Use System.Reflection to iterate over properties and fields of the base class and apply them to the derived class. This example includes a single public property and single public field, but will also work with multiple private/protected properties and fields. You can paste the entire example into a new console application to test it.
Imports System.Reflection
Module Module1
Sub Main()
Dim p As New Parent
p.Property1 = "abc"
p.Field1 = "def"
Dim c = New Child(p)
Console.WriteLine("Property1 = ""{0}"", Field1 = ""{1}""", c.Property1, c.Field1)
Console.ReadLine()
End Sub
Class Parent
Public Property Property1 As String = "not set"
Public Property Field1 As String = "not set"
End Class
Class Child
Inherits Parent
Public Sub New(myParent As Parent)
Dim fieldInfo = GetType(Parent).GetFields(BindingFlags.NonPublic _
Or BindingFlags.Instance)
For Each field In fieldInfo
field.SetValue(Me, field.GetValue(myParent))
Next
Dim propertyInfo = GetType(Parent).GetProperties(BindingFlags.NonPublic _
Or BindingFlags.Instance)
For Each prop In propertyInfo
prop.SetValue(Me, prop.GetValue(myParent))
Next
End Sub
End Class
End Module
Output:
Property1 = "abc", Field1 = "def"
This solution is automated, so you won't need to change anything when adding or removing properties and fields in the base class.
In general, because of this:
Class TheBase
End Class
Class Derived1 : TheBase
Sub Foo()
End Sub
End Class
Class Derived2 : TheBase
Sub Bar()
End Sub
End Class
Sub Main()
Dim myDerived1 As New Derived1
' cast derived to base
Dim myTheBase = CType(myDerived1, TheBase)
' cast base to derived?
' but myTheBase is actually a Derived1
Dim myDerived2 As Derived2 = CType(myTheBase, Derived2)
' which function call would you like to succeed?
myDerived2.Foo()
myDerived2.Bar()
End Sub

Access static methods and attributes of the friend class in ABAP

This is how my two global classes look like:
CLASS zcl_singleton_class DEFINITION CREATE PRIVATE friends ZCL_FLINFO
PUBLIC SECTION.
CLASS-METHODS:
CLASS_CONSTRUCTOR,
get_instance
RETURNING VALUE(r_instance) TYPE REF TO zcl_singleton_class.
PRIVATE SECTION.
types:
TY_CONNECTION_LIST TYPE STANDARD TABLE OF SPFLI WITH KEY carrid connid.
class-data instance type ref to zcl_singleton_class .
class-data CONNECTION_LIST type TY_CONNECTION_LIST .
ENDCLASS.
CLASS zcl_singleton_class IMPLEMENTATION.
method CLASS_CONSTRUCTOR.
instance = instance.
SELECT * FROM SPFLI INTO TABLE CONNECTION_LIST.
endmethod.
METHOD get_instance.
r_instance = instance.
ENDMETHOD.
ENDCLASS.
CLASS ZCL_FLINFO DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
CLASS_CONSTRUCTOR,
get_connection
IMPORTING im_carrid type S_CARR_ID
RETURNING VALUE(re_connection) TYPE.
ENDCLASS.
CLASS ZCL_FLINFO IMPLEMENTATION.
METHOD get_connection.
LOOP at CONNECTION_LIST TRANSPORTING NO FIELDS WHERE carrid = im_carrid.
re_connection = re_connection + 1.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
How can I implement the get_connection method of ZCL_FLINFO so it to iterate through the internal table CONNECTION_LIST of zcl_singleton_class, to count the number of connections for the given airline and return it in the parameter?
I figured out something the work out fine in my case. If a class A(zcl_singleton_class) offers friendship to another class B(ZCL_FLINFO), B can access the private components of A. So, I simply access the internal table(CONNECTION_LIST) by calling it in my loop.
method GET_N_O_CONNECTIONS.
LOOP AT zcl_singleton_class=>CONNECTION_LIST TRANSPORTING NO FIELDS WHERE CARRID = IM_CARRID.
RE_N_O_CONNECTIONS = RE_N_O_CONNECTIONS + 1.
ENDLOOP.
endmethod.

How to pass parameters to a Progress program using database field dynamic-based rules?

I have in my database a set of records that concentrates information about my .W's, e.g. window name, parent directory, file name, procedure type (for internal treatments purposes), used to build my main menu. With this data I'm developing a new start procedure for the ERP that I maintain and using the opportunity in order to rewrite some really outdated functions and programs and implement new functionalities. Until now, I hadn't any problems but when I started to develop the .P procedure which will check the database register of a program that was called from the menu of this new start procedure - to check if it needs to receive fixed parameters to be run and its data types - I found a problem that I can't figure out a solution.
In this table, I have stored in one of the fields the parameters needed by the program, each with his correspondent data type. The problem is on how to pass different data types to procedures based only on the stored data. I tried to pre-convert data using a CASE clause and an include to check the parameter field for correct parameter sending but the include doesn't work as I've expected.
My database field is stored as this:
Description | DATATYPE | Content
I've declared some variables and converted properly the stored data into their correct datatype vars.
DEF VAR c-param-exec AS CHAR NO-UNDO EXTENT 9 INIT ?.
DEF VAR i-param-exec AS INT NO-UNDO EXTENT 9 INIT ?.
DEF VAR de-param-exec AS DEC NO-UNDO EXTENT 9 INIT ?.
DEF VAR da-param-exec AS DATE NO-UNDO EXTENT 9 INIT ?.
DEF VAR l-param-exec AS LOG NO-UNDO EXTENT 9 INIT ?.
DEF VAR i-count AS INT NO-UNDO.
blk-count:
DO i-count = 0 TO 8:
IF TRIM(programa.parametro[i-count]) = '' THEN
LEAVE blk-count.
i-count = i-count + 1.
CASE ENTRY(2,programa.parametro[i-count],CHR(1)):
WHEN 'CHARACTER' THEN
c-param-exec[i-count] = ENTRY(3,programa.parametro[i-count],CHR(1)).
WHEN 'INTEGER' THEN
i-param-exec[i-count] = INT(ENTRY(3,programa.parametro[i-count],CHR(1))).
WHEN 'DECIMAL' THEN
de-param-exec[i-count] = DEC(ENTRY(3,programa.parametro[i-count],CHR(1))).
WHEN 'DATE' THEN
da-param-exec[i-count] = DATE(ENTRY(3,programa.parametro[i-count],CHR(1))).
WHEN 'LOGICAL' THEN
l-param-exec[i-count] = (ENTRY(3,programa.parametro[i-count],CHR(1)) = 'yes').
OTHERWISE
c-param-exec[i-count] = ENTRY(3,programa.parametro[i-count],CHR(1)).
END CASE.
END.
Then I tried to run the program using an include to pass parameters (in this example, the program have 3 INPUT parameters).
RUN VALUE(c-prog-exec) ({util\abrePrograma.i 1},
{util\abrePrograma.i 2},
{util\abrePrograma.i 3}).
Here is my abrePrograma.i
/* abrePrograma.i */
(IF ENTRY(2,programa.parametro[{1}],CHR(1)) = 'CHARACTER' THEN c-param-exec[{1}] ELSE
IF ENTRY(2,programa.parametro[{1}],CHR(1)) = 'INTEGER' THEN i-param-exec[{1}] ELSE
IF ENTRY(2,programa.parametro[{1}],CHR(1)) = 'DECIMAL' THEN de-param-exec[{1}] ELSE
IF ENTRY(2,programa.parametro[{1}],CHR(1)) = 'DATE' THEN da-param-exec[{1}] ELSE
IF ENTRY(2,programa.parametro[{1}],CHR(1)) = 'LOGICAL' THEN l-param-exec[{1}] ELSE
c-param-exec[{1}])
If I suppress the 2nd, 3rd, 4th and 5th IF's from the include or use only one data type in all IF's (e.g. only CHAR, only DATE, etc.) the program works properly and executes like a charm but I need to call some old programs, which expects different datatypes in its INPUT parameters and using the programs as described OpenEdge doesn't compile the caller, triggering the error number 223.
---------------------------
Erro (Press HELP to view stack trace)
---------------------------
** Tipos de dados imcompativeis em expressao ou atribuicao. (223)
** Nao entendi a linha 86. (196)
---------------------------
OK Ajuda
---------------------------
Can anyone help me with this ?
Thanks in advance.
Looks as if you're trying to use variable parameter definitions.
Have a look at the "create call" statement in the ABL reference.
http://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/dvref/call-object-handle.html#wwconnect_header
Sample from the documentation
DEFINE VARIABLE hCall AS HANDLE NO-UNDO.
CREATE CALL hCall.
/* Invoke hello.p non-persistently */
hCall:CALL-NAME = "hello.p".
/* Sets CALL-TYPE to the default */
hCall:CALL-TYPE = PROCEDURE-CALL-TYPE
hCall:NUM-PARAMETERS = 1.
hCall:SET-PARAMETER(1, "CHARACTER", "INPUT", "HELLO WORLD").
hCall:INVOKE.
/* Clean up */
DELETE OBJECT hCall.
The best way to get to the bottom of those kind of preprocessor related issues is to do a compile with preprocess listing followed by a syntax check on the preprocessed file. Once you know where the error is in the resulting preprocessed file you have to find out which include / define caused the code that won't compile .
In procedure editor
compile source.w preprocess source.pp.
Open source.pp in the procedure editor and do syntax check
look at original source to find include or preprocessor construct that resulted in the code that does not compile.
Okay, I am getting a little bit lost (often happens to me with lots of preprocessors) but am I missing that on the way in and out of the database fields you are storing values as characters, right? So when storing a parameter in the database you have to convert it to Char and on the way out of the database you have convert it back to its correct data-type. To not do it one way or the other would cause a type mismatch.
Also, just thinking out loud (without thinking it all the way through) wonder if using OOABL (Object Oriented ABL) depending on if you Release has it available wouldn't make it easier by defining signatures for the different datatypes and then depending on which type of input or output parameter you call it with, it will use the correct signature and correct conversion method.
Something like:
METHOD PUBLIC VOID storeParam(input cParam as char ):
dbfield = cParam.
RETURN.
END METHOD.
METHOD PUBLIC VOID storeParam(input iParam as int ):
dbfield = string(iParam).
RETURN.
END METHOD.
METHOD PUBLIC VOID storeParam(input dParam as date ):
dbfield = string(dParam).
RETURN.
END METHOD.
just a thought.

Is a "data transfer type" the same as a "data transfer object"?

In reading about C#, I have come across the terms "data transfer type" and "data transfer object". This shows up around annonymous types, where a type is created on the fly to hold results, such as from LINQ. Are these two terms referring to the same thing ?
Thanks,
Scott
I think some more context would help here.
An anonymous type has method scope. So this means, it cannot be passed outside of it's method. Whereas a Data Transfer Object entire purpose in life is to be passed outside of it's method.
I suspect their creating Data Transfer Types through an anonymous type and then projecting this to a Data Transfer Object.
But yeah, including the sentence you found this term in would help.
The type is the description of the object, it's class and it's methods/properties/variables/...
while the object is an instance of the type.
For example:
// this describes the type Foo
public sealed class Foo
{
public int ID { get; set; }
/* ... */
}
// this is an object (instance) of foo
var fooInstance = new Foo() { ID = 4, };