Skip to content

A Char passed to an Integer parameter of another object's method fails at run time: "Unable to convert from System.Char to System.Int32" #8374

Open

Description

Please include the following with each issue:

1. Describe the bug
AL converts a Char to Integer implicitly. When the Char argument goes to a method of another object (called through Invoke with an object[]), the compiler puts the .NET char in the argument array without converting it to int, and the callee fails at run time when it converts its arguments. There is no compile-time diagnostic, and the server compiles the generated C#. A call to a procedure of the same object is a direct C# call and works.

2. To Reproduce
Steps to reproduce the behavior:

  1. Create an app ("runtime": "15.0") with the two codeunits below.
  2. Publish it to a Business Central 29 sandbox.
  3. Run codeunit 50102 (e.g. add &codeunit=50102 to the web client URL).
  4. Optionally, build with alc /generatecode+ and open bin/COD50102.cs in the .app to see the generated C#.
codeunit 50102 "Char Argument Repro"
{
    trigger OnRun()
    var
        Receiver: Codeunit "Char Receiver";
    begin
        Receiver.Take(this.GetText()[1]);
    end;

    local procedure GetText(): Text
    begin
        exit('A');
    end;
}

codeunit 50103 "Char Receiver"
{
    procedure Take(Code: Integer)
    begin
        Message('%1', Code);
    end;
}

3. Expected behavior
Take receives 65 and shows it. The argument should be converted to the parameter's type before it is put in the argument array, as the compiler already does for an assignment to an Integer:

// CharCode := this.GetText()[1];  Receiver.Take(CharCode);
this.charCode = base.Parent.GetText()[1 - 1];
this.receiver.Target.Invoke(-761693749, new object[] { this.charCode });

4. Actual behavior
Generated C# (from alc /generatecode+):

this.receiver.Target.Invoke(-761693749, new object[] { base.Parent.GetText()[1 - 1] });

Error at run time (observed in our app, whose generated code has exactly this pattern; the repro above generates the same line):

Unable to convert from System.Char to System.Int32.

The same generated code (not run) appears for a character of a Text variable (new object[] { this.source[1 - 1] }) and for a Char variable (new object[] { this.character }). A call to a procedure of the same object is not affected (base.Parent.TakeLocal(this.source[1 - 1]) is a direct call with the implicit C# conversion).

Workaround: assign the Char to an Integer variable first and pass the variable.

5. Versions:

  • AL Language: 18.1.2842859 (compiler alc.exe 18.1.43.24897)
  • Visual Studio Code: 1.142.0
  • Business Central: online sandbox, version 29 (platform 29.0.57071.0)
  • List of Visual Studio Code extensions that you have installed: not involved. The repro was built with the command-line compiler (alc.exe from the AL Language extension's bin folder), outside Visual Studio Code.
  • Operating System:
    • Windows
    • Linux
    • MacOS

Final Checklist

Please remember to do the following:

  • Search the issue repository to ensure you are reporting a new issue

  • Reproduce the issue after disabling all extensions except the AL Language extension

  • Simplify your code around the issue to better isolate the problem

Internal work item: AB#658297

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions