代码之家  ›  专栏  ›  技术社区  ›  Brad

Axapta:强制将容器整数存储为字符串

  •  1
  • Brad  · 技术社区  · 16 年前

    有没有办法强制容器将所有值存储为字符串?我使用str2con将文本字符串拆分为容器。 只要出现一个只有数字的字段,它就会被存储为int,这不是什么大问题。一个大问题是当数字串超过整数大小时,数字会变得不同。

    考虑以下字符串:

    "Text1,Text2"      Container becomes: str "Text1", str "Text2"
    "1111111111,Text"   Container becomes: int 1111111111, str "Text"   
    "8888888888,Text"   Container becomes: int -961633963, str "Text"  (THIS IS BAD)
    

    有什么建议可以解决这个问题吗?

    谢谢

    3 回复  |  直到 16 年前
        1
  •  2
  •   Jay Hofacker    16 年前

    通过查看Global.str2con方法,可以了解Microsoft是如何实现str2con的。为了防止该方法向容器中添加整数,请复制该方法,并只注释掉add2Ret子函数中检查字符串是否仅为数字的三行。您可能不想修改现有的str2con函数,因为系统的其他部分在调用此方法时可能依赖于实际为整数的整数。

    void add2Ret(str _current)
    {  
        // v-artemt, 26 Jul 2004, PS#: 1741
     //remove next three lines so only integers will be added as strings not integers
     //   if (match('<:d+>', _current))
     //       ret += str2int(_current);
     //   else
            ret += _current;
    }
    

    或者,您可以添加更复杂的逻辑来检查字符串的长度,并且仅当字符串可能适合整数时才使用str2int。

        2
  •  1
  •   ceth    16 年前

    以下是替代实现:

    #define.Comma(",")
    
    static container str2con_alt(str _string, str _separator = #Comma, boolean  _ignoreNearSeparator = false)
    {
      container con = connull();
      int       pos, oldPos = 1;
      str       tmpStr;
    
      do
      {
          pos    =  strscan(_string, _separator, pos ? pos + strlen(_separator) : 1, strlen(_string));
          tmpStr =  substr(_string, oldPos, pos ? pos - oldPos : strlen(_string) + 1 - oldPos);
    
          if (tmpStr || ! _ignoreNearSeparator)
          {
             con += tmpStr;
          }
    
          oldPos =  pos + strlen(_separator);
      }
      while (pos);
    
      return con;
    }
    
        3
  •  0
  •   Felipe Nogueira    8 年前

    全球的:: STR2CONU RU () 我曾经使用过这种方法,我相信大多数环境都有这种方法。

    ledgerCon=Global::str2 con_RU(ledgerDimStr,#sep);