Bil*_*eed 4 collections x++ axapta dynamics-ax-2012
我想用X ++存储对象列表.我在msdn中读到数组和容器不能存储对象,因此唯一的选择是创建一个Collection列表.我写了下面的代码,并试图用Collection = new List(Types::AnyType);和Collection = new List(Types::Classes);,但双方都没有工作.请看看我是否在以下工作中犯了一些错误.
static void TestList(Args _args)
{
List Collection;
ListIterator iter;
anytype iVar, sVar, oVar;
PlmSizeRange PlmSizeRange;
;
Collection = new List(Types::AnyType);
iVar = 1;
sVar = "abc";
oVar = PlmSizeRange;
Collection.addEnd(iVar);
Collection.addEnd(sVar);
Collection.addEnd(oVar);
iter = new ListIterator(Collection);
while (iter.more())
{
info(any2str(iter.value()));
iter.next();
}
}
Run Code Online (Sandbox Code Playgroud)
而且,我们不能将一些变量或对象转换为Anytype变量,我读出类型转换是以这种方式自动完成的;
anytype iVar;
iVar = 1;
Run Code Online (Sandbox Code Playgroud)
但是在运行它时抛出一个错误,期望类型是Anytype,但遇到的类型是int.
最后,anytype变量采用首先分配给它的类型,以后不能更改它:
static void Job2(Args _args)
{
anytype iVar;
iVar = 1; //Works, iVar is now an int!
iVar = "abc"; //Does not work, as iVar is now bound to int, assigns 0
info(iVar);
}
Run Code Online (Sandbox Code Playgroud)
回到第一个问题,new List(Types::AnyType)将永远不会工作,因为addEnd方法在运行时测试其参数的类型,anytype变量将具有分配给它的值的类型.
也new List(Types::Object)只能存储对象,而不是简单的数据类型int和str.它可能与您(和C#)所相信的相反,但简单类型不是对象.
剩下什么?集装箱:
static void TestList(Args _args)
{
List collection = new List(Types::Container);
ListIterator iter;
int iVar;
str sVar;
Object oVar;
container c;
;
iVar = 1;
sVar = "abc";
oVar = new Object();
collection.addEnd([iVar]);
collection.addEnd([sVar]);
collection.addEnd([oVar.toString()]);
iter = new ListIterator(collection);
while (iter.more())
{
c = iter.value();
info(conPeek(c,1));
iter.next();
}
}
Run Code Online (Sandbox Code Playgroud)
对象不会自动转换为容器,通常是您提供pack和unpack方法(实现接口SysPackable).在上面的代码toString中使用的是作弊.
另一方面,我没有看到您的请求的用例,列表应包含任何类型.它违背了其设计目的,List包含一个且只有一个类型,在创建List对象时定义.
除了列表之外还有其他集合类型,也许Struct可以满足您的需求.
| 归档时间: |
|
| 查看次数: |
5540 次 |
| 最近记录: |