Farseer

导航

D365 FO最佳实践BP(六)-格式化字符串

X++里格式化字符串使用strFmt函数,字符串里面的替代符号用%1表示,比如

1 strFmt("%1 World", Hello);

类似于C#里的string.Format,只不过C#里使用{0}做替代符号。
在C#里,如果{N} N大于了后面字符串的数目,在运行期会报错,如下代码:

 Console.WriteLine(string.Format("{0}{1}", "Hello"));
 Console.ReadLine();

 X++里这种情况不会运行时错误,但是会显示为%N这种,这样已经起不到提示用户的作用了。
BP检查里有两条
BPFormatSpecifierInvalid
The number of parameters (1) doesn't match with the number of format specifiers: (2)
当%N大于后面的字符串个数时,会报上述BP。

BPUnusedStrFmtArgument
The placeholder '%3' to strFmt is not used in the format string.
当%N小于后面的字符串个数时,会报上述BP。
通过这个BP检查就可以避免strFmt参数不匹配的情况了。

posted on 2020-06-02 08:43  佛西亚  阅读(295)  评论(0编辑  收藏  举报