I am trying to do a typical Get procedure with this features:
Header:
procedure Get (M:Map; Key: in Key_Type; Value: out Value_Type; Success: out Boolean) is
private part:
type Cell is record
Key: Key_Type;
Value: Value_Type;
Full:Boolean:= False;
end record
type Cell_Array is array (1..50) of Cell;
type Cell_Array_A is access Cell_Array;
type Map is record
P_Array: Cell_Array_A;
Length:Natural=0;
Indice:Natural;
end record
My first condition on my Get procedure has the following aspect:
for k in 1..NumClien loop
if M.P_Array(k).Key = Key then
...
But when I try to execute it, it always gives me the following error:
Lower_Layer.Inet(Receiver_Task): Unexpected exception raised CONSTRAINT_ERROR
How can I solve it?
PS: Header of the Get procedure and private part are mandatory...
Thank you!
if you eliminate the unnecessary use of access types, your problem will go away:
type Map is record
List : Cell_Array;
...
end record;
Access types are needed so rarely in well designed Ada that I'm tempted to say "never".
Related
Working on an application that relies on an older version of entity, and I'm trying to insert a NULL into an int field. The field in SQL Server is (int, null).
Here's the definition of the object in EF:
<EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
<DataMemberAttribute()>
Public Property application_id() As Nullable(Of Global.System.Int32)
...and here is where I'm trying to set it:
applications.application_id = IIf(IsNumeric(txtAppID.Text), CInt(txtAppID.Text), Nothing)
The error thrown in response is:
An exception of type 'System.InvalidCastException' occurred in ... but was not handled in user code
Additional information: Specified cast is not valid.
I can confirm that this issue is being thrown due to the Nothing portion because previously it was applications.application_id = CInt(txtAppID.Text) and all was fine.
I've tried DBNull.Value instead of Nothing, though the error reads the same. Done a fair bit of research though most issues relate to ES6 or datetime fields, and as such I felt my issue was specific enough to warrant its own question.
Thanks.
The IIf function doesn't short circuit, and therefore always evaluates both the true and false parts, so it's not going to work in that situation. The If keyword does short circuit, but you will probably run into issues with the return type and nullable value types (e.g. Dim x As Integer? = If(False, 1, Nothing) results in x = 0 because the If is returning Integer and not Integer?).
So, I would recommend either using a regular If statement:
If IsNumeric(txtAppID.Text) Then
applications.application_id = CInt(txtAppID.Text)
Else
applications.application_id = Nothing
End If
or you could create a helper function:
Function NullableCInt(value As String) As Integer?
If IsNumeric(value) Then Return CInt(value)
Return Nothing
End Function
and use that:
applications.application_id = NullableCInt(txtAppID.Text)
You can get working If method with casting
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DirectCast(Nothing, Integer?))
For better readability you can introduce "default" value
Static DEFAULT_VALUE As Integer? = Nothing
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DEFAULT_VALUE)
With Integer.TryParse you need "check/convert" string to integer only once.
I am trying to create a method to insert data to the database but it does not work. I am trying to insert data by using add_person. Here is my code. The transparent table name is ZPERSON_20.
CLASS lcl_person DEFINITION.
PUBLIC SECTION.
METHODS:
add_person
IMPORTING
im_id TYPE zperson_20-person_id
im_name TYPE zperson_20-person_name
im_add TYPE zperson_20-person_address
im_type TYPE zperson_20-person_type.
PRIVATE SECTION.
DATA:
c_id TYPE zperson_20-person_id,
c_name TYPE zperson_20-person_name,
c_add TYPE zperson_20-person_address,
c_type TYPE zperson_20-person_type.
ENDCLASS.
CLASS lcl_person IMPLEMENTATION.
METHOD add_person.
DATA: it_emp TYPE STANDARD TABLE OF zperson_20.
DATA: wa_emp LIKE LINE OF it_emp.
wa_emp-person_id = c_id.
wa_emp-person_name = c_name.
wa_emp-person_add = c_add.
wa_emp-person_type = c_type.
INSERT INTO zperson_20 VALUES wa_emp.
ENDMETHOD.
ENDCLASS.
PARAMETERS:
v_id TYPE zperson_20-person_id,
v_name TYPE zperson_20-person_name,
v_add TYPE zperson_20-person_address,
v_type TYPE zperson_20-person_type.
DATA: lv_ref_person TYPE REF TO lcl_person.
START-OF-SELECTION.
CREATE OBJECT lv_ref_person.
CALL METHOD lv_ref_person->add_person(
im_id = v_id
im_name = v_name
im_add = v_add
im_type = v_type
).
I would say your issue is inside the add_person method, as you are reading the values from the member variables, instead of the method parameters. Those member variables will probably be empty in this case, leading to inserting errors due to an empty or duplicated id.
I believe that the code you are trying to achieve is the following:
REPORT ZRKD_BASE_WSLOADER.
CLASS lcl_person DEFINITION.
PUBLIC SECTION.
METHODS:
add_person
IMPORTING
im_id TYPE zperson_20-person_id
im_name TYPE zperson_20-person_name
im_add TYPE zperson_20-person_address
im_type TYPE zperson_20-person_type.
ENDCLASS.
CLASS lcl_person IMPLEMENTATION.
METHOD add_person.
DATA: wa_emp TYPE zperson_20.
//----> Here are the modifications <-------
wa_emp-person_id = im_id.
wa_emp-person_name = im_name.
wa_emp-person_address = im_add.
wa_emp-person_type = im_type.
INSERT INTO zperson_20 VALUES wa_emp.
ENDMETHOD.
ENDCLASS.
PARAMETERS:
v_id TYPE zperson_20-person_id,
v_name TYPE zperson_20-person_name,
v_add TYPE zperson_20-person_address,
v_type TYPE zperson_20-person_type.
DATA: lv_ref_person TYPE REF TO lcl_person.
START-OF-SELECTION.
CREATE OBJECT lv_ref_person.
CALL METHOD lv_ref_person->add_person(
im_id = v_id
im_name = v_name
im_add = v_add
im_type = v_type
).
Would be enough to create a commit method, which you then call, if You are really sure to save on db, or add this parameter as a flag to add person. If set, commit the transaction. The instruction for this is "commit work." and it is missing in the entire snippet. And without it nothing moves from Your actual LUW commit buffer to db.
Your method add_person is defined to take no arguments and you are passing it arguments. Basically there is a disagreement in call and the definition.
Can you post the whole code here?
Is there a way to parse enum value from table record. For example, I have class which contains user data, and on of them is Enum type. The data is passed from DataRow, but I have trouble parsing enum value.
I tried something like this,
uType= (EType) Enum.TryParse(typeof(row["userType"]));
but it wouldn't compile. Any tip?
Thanks.
try with this code
uType = (EType) Enum.Parse(typeof(EType), row["userType"].ToString(), true);
Enum.TryParse returns a boolean that indicates if the value could be parsed successfully.
Assuming that userType is a string in the DataTable:
EType eType;
bool canParse = Enum.TryParse(row.Field<String>("userType"), out eType);
I'm trying to pass an array out of a class into my main program in Delphi. I'm having a bit of trouble with the data types and an hour scouring the web has found nothing to help me. It sounds a bit strange, but the more complex the answer the better (it's for a college project).
I have a class connected to SQL which reads an SQL Query into an array of a record Type declarations (sorry if it's a bit messy at the moment) :
Type TScout = Record
SNum, FName, SName, Gender, Address, HomeNum, MobNum,
SEmail, STel, Hikes, Nights, Med, Diet : String;
DoB, DoJ : String;
End;
Type TScoutArray = Array of TScout;
Type TScoutSQL = Class
Public
Procedure InitSQL;
Procedure GetRecords;
Function SendRecords : TScoutArray;
Private
ScoutsArray : TScoutArray;
ScoutConnection : TSQLConnection;
ScoutQuery : TSQLQuery;
End;
So the whole "Function SendRecords : TScoutArray;" isn't working, as on the other side I have the same 2 types (TScout and TScoutArray) declared exactly the same, I call the function:
ScoutArray := ScoutSQL.SendRecords;
And I get:
[Error] MembersUnit.pas(51): Incompatible types
Can anyone help?
I suspect that your problem is that you are declaring these types twice in separate units. Doing so results in distinct, incompatible types.
What you need to do is to:
Declare the types, in the interface section, of one unit only (unit A, say).
In another unit (unit B, say) that wants to use these types you add unit A to the uses clause.
Wrong way
var
badArray: array of TScout;
begin
badArray := ScoutSQL.SendRecords;
This won't work. array of TScout and TScoutArray, the latter being what SendRecords returns, are different types in Delphi/Pascal.
Right way
var
niceArray: TScoutArray;
begin
niceArray := ScoutSQL.SendRecords;
I have just 2 objects and simple query to retrieve the data.
The result of query which is stored in array ccList according to debug output is:
(
CustomThree__c:
{
Name=cusmei3 2,
customOne__c=a005000000IwnOPAAZ,
Id=a025000000FsFGQAA3
},
CustomThree__c:
{
Name=cusmei3 1,
customOne__c=a005000000IwnOUAAZ,
Id=a025000000FsFGLAA3
}
)
As you can see system.debug(ccList[0]) returns:
CustomThree__c:{
Name=cusmei3 2,
customOne__c=a005000000IwnOPAAZ,
Id=a025000000FsFGQAA3
}
But when I try to get Id (or other field) from the array, the error occurs.
Can anyone point out what am I doing wrong?
code
Object[] ccList;
ccList = [SELECT id, name, CustomOne__r.name FROM CustomThree__c];
system.debug(ccList);
system.debug('******************************************');
system.debug(ccList[0]);
system.debug(ccList[0].Id); //this one cause the error
I think you'll have to change the type of ccList from "Object" to "CustomThree__c". This will also give you compile-time checking when you'll try to write ccList[0].SomeNonExistentFieldName__c.
If you can't do it and really need the object that stores result to be generic - I believe this should be SObject?