Code data type can specify index like dimension??

teckpohteckpoh Member Posts: 271
Dear all,
i upgraded my nav from v3 to v5SP1 then i noted my customized code in v3 was not working in v5 the coding is as below:

GetPrefix(Param_Cust : Record Customer) : Code[2]
IF Param_Cust.Type = Param_Cust.Type::Members THEN BEGIN
Var_Result[1] := 'K';
IF Param_Cust.Area <> '' THEN Var_Result[2] := Param_Cust.Area[1];
END;
IF Param_Cust.Type = Param_Cust.Type::Students THEN BEGIN
Var_Result[1] := 'S';
//MESSAGE(FORMAT(Var_Result[1]));
Var_Cust.RESET;
IF Var_Cust.GET(Param_Cust."Student's Parent Member No.") THEN
IF Var_Cust.Area <> '' THEN Var_Result[2] := Var_Cust.Area[1];
END;
EXIT(Var_Result);


i noted "Var_Result" datatype=code and its dimension =0 i juz curious how come we can specify Var_Result[1] or [2] to it since its dimension =0...and i do wonder whether v3 support index[1] or [2] for code..and it was not support by v5..coz when i run in v3..the EXIT(Var_Result) has value but in v5 the EXIT(Var_Result) return null value...correct me if i'm wrong.. :)

Comments

  • kinekine Member Posts: 12,562
    Each string (text or code) is in real "Array of chars". It means you can use the indexes to access each char in the string. That's ok and is working.


    But to be able to assign the chars, you need to create string with correct length. Try to assign some string to the variable first, and after that you will be able to assign specific char to each position.

    It means, try to do something like this first:
    Var_result := '           ';
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • teckpohteckpoh Member Posts: 271
    GetPrefix(Param_Cust : Record Customer) : Code[2]
    Var_Result[1] :='y';
    Var_Result[2] :='z';
    MESSAGE('%1....%2',Var_Result[1],Var_Result[2]);
    MESSAGE(Var_Result);
    EXIT(Var_Result);

    hi kine, 10s 4 d replied...i use above code for my testing. The 1st message line(MESSAGE('%1....%2',Var_Result[1],Var_Result[2]);) gave me the value BUT second message line gave me blank value..how come ar? #-o
    and the EXIT(Var_Result) shown blank as well... :( Any idea?? 10s in adv
  • kinekine Member Posts: 12,562
    teckpoh wrote:
    GetPrefix(Param_Cust : Record Customer) : Code[2]
    Var_Result[1] :='y';
    Var_Result[2] :='z';
    MESSAGE('%1....%2',Var_Result[1],Var_Result[2]);
    MESSAGE(Var_Result);
    EXIT(Var_Result);

    hi kine, 10s 4 d replied...i use above code for my testing. The 1st message line(MESSAGE('%1....%2',Var_Result[1],Var_Result[2]);) gave me the value BUT second message line gave me blank value..how come ar? #-o
    and the EXIT(Var_Result) shown blank as well... :( Any idea?? 10s in adv

    Try this:
    Var_Result := '  ';
    Var_Result[1] :='y';
    Var_Result[2] :='z';
    MESSAGE('%1....%2',Var_Result[1],Var_Result[2]);
    MESSAGE(Var_Result);
    EXIT(Var_Result);
    

    In PASCAL (which is base language for C/AL), the string internal representation is, that on the first position (first byte of the string, index 0) is saved length of the string, after that each byte is one char. If you assign only the chars, you still have wrong string length. If you assign first some string, the length will be set to correct value and thus functions using the string will show correct output. Else the string is still "0" length, regardless you set some "char" of the string to some value. This is how it works in PASCAL. In C/AL it works in similar way, of course, because there are strings longer than 255 bytes, the internal representation is not in bytes, but it seems they are in words...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • teckpohteckpoh Member Posts: 271
    i tried...and MESSAGE(Var_Result) still gave me blank value...
  • kinekine Member Posts: 12,562
    Than problem must be somewhere else. For me it is working on all versions I tested it (4SP3, 5SP1).

    Did you tried to debug it?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.