microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
744 stars 245 forks source link

MaxStrLen Deprecated? #4667

Closed f4n0 closed 5 years ago

f4n0 commented 5 years ago

Hi, I'm trying to resolve some CodeCop error but I'm stuck here: I was used to solving the error "Possible overflow assigning 'Text' to 'Code[128]'. (AL0139)" simply by adding a CopyStr(myVar,1,MaxStrLen(MyVar)). But today I found out this Warning message:

{
    "resource": "",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "AL0141",
    "severity": 4,
    "message": "'MaxStrLen' is deprecated",
    "source": "AL",
    "startLineNumber": 55,
    "startColumn": 81,
    "endLineNumber": 55,
    "endColumn": 104
}

Assuming that I would not like to implement some hard-coded code (like CopyStr(myVar,1, MaxStrLen(50)) ) also to make my code easier to maintain, How can I fix that?

Thanks Stefano Primo

f4n0 commented 5 years ago

I leave an Update: I was watching my code where throws this warning and I saw this: image

Now I'm a bit disappointing: If you watch carefully you will see the MaxStrLen that throws the warning and a few lines before there is also another MaxStrLen that does not throw any type of error/warning.

thpeder commented 5 years ago

Hi @stefanoPrimo, The reason why you are getting an error on one and not the other is because the method has an overload. The first takes a text and the second a variant. The method that takes a variant results in a error being raised no matter what, so you need to change it to a text if you want to use that method.

f4n0 commented 5 years ago

Hi @thpeder, Thanks a lot, I didn't saw or knew that the MaxStrLen method could have an overload.