哈哈,我这样实现了:
dim collection_temp as new collection
dim class_temp as class1set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_tempset class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp不过我不理解 set new 实例的生存期

解决方案 »

  1.   

    可不可以這樣
    dim a1 as new class1
    dim a2 as new class2
      

  2.   

    可以这么办
    dim class_temp as class1
    set class_temp=new class1
    collection_temp.add class_temp
    '其他代码
    set class_temp=new class1
    collection_temp.add class_temp
      

  3.   

    你new出来的class
    当所有指向它的引用都设成nothing
    则该对象就会被释放
      

  4.   

    當定義時, set new 实例的生存期
    開始
    當你結束對象時, set new 实例的生存期
    就結束
      

  5.   

    我指向它的引用=nothing 就释放,而指向另一个实例就不释放吗??????????
      

  6.   

    不是很清楚
    可能不会
    所以保险一点当用完以后都要set =nothing
    否则内存会溢出
      

  7.   

    至少createobject出来的东西是不会自动释放的
      

  8.   

    createobject 怎么用,声明class1实例。代码怎么写?????
      

  9.   

    set =nothing
    到底怎么回事,我要写一个类似服务的程序,可不能跑没两天就拖跨机器了!!!!!
      

  10.   

    如果是我会这么写
    dim collection_temp as new collection
    dim class_temp as class1
    set class_temp as new class1
    class_temp.aa="aaa"
    collection_temp.add class_temp
    set class_temp=nothingset class_temp as new class1
    class_temp.aa="bbb"
    collection_temp.add class_temp
    set class_temp=nothing仅供参考
      

  11.   

    查了一下
    new是会自动帮你释放原来的引用
    但是最后一个set=nothing不能少关于nothing,msdn有以下论述,可作参考
    Set MyObject = Nothing
    Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope. 
      

  12.   

    dim collection_temp as new collection
    dim class_temp as class1
    set class_temp as new class1
    class_temp.aa="aaa"
    collection_temp.add class_temp
    'set class_temp=nothing可以没有set class_temp as new class1
    class_temp.aa="bbb"
    collection_temp.add class_temp
    set class_temp=nothing'一定要
      

  13.   

    楼上的方法我想改进一下,做一个方法,把set=nothing封装起来,
    在wrap一个index属性,这样可以根据动态生成的类的index去结束相应的类
    便于管理
      

  14.   

    令我不解的是,你既然能考虑到做一个collection,为什么还会问set..=nothing怎么用
    另外,你们的collection为什么不把自定义的class包进去呢---即让你的类成为collection
    的item,否则,在这里用collection对象有什么意义
      

  15.   

    系统对于这种资源的回收,我知道的有2种方式,
    一种是引用计数,就是楼上所说的那个意思,有个计数属性 set的时候+1,set =nothing时-1,=0释放自己
    还有一种叫做垃圾回收,就是到资源使用超出一定的范围,系统来一次scan看看那些对象已经没有引用指向它了,然后释放
    但这些实现不用你考虑,只需要了解就行了
      

  16.   

    dim collection_temp as new collection
    dim class_temp as class1
    set class_temp as new class1
    class_temp.aa="aaa"
    collection_temp.add class_temp
    set class_temp=nothingset class_temp as new class1
    class_temp.aa="bbb"
    collection_temp.add class_temp
    set class_temp=nothing
    这样加一个 nothing一个不行,nothing后不能访问原指向的项目。
    ”new是会自动帮你释放原来的引用,但是最后一个set=nothing不能少“,这样看来也讲不通。
      

  17.   

    我这话也不是凭空说出来的
    一下是摘自msdn
    Set objectvar = {objectexpression | New classname | Nothing}NewKeyword used to create a new instance of a class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can only be used to create an instance of a class.
      

  18.   

    上面的意思是说,如果new的时候变量已经指向一个对象,那么new的时候就会释放一个引用
    相当于自动在前面加一句set =nothing
    最后要加是因为如果声明class_temp所在的函数结束了,则该变量就会被释放,而引用并没有被释放
      

  19.   

    set class_temp=nothing
    的时候其实对象并没有释放,因为在collection里面还有一个引用是指向它的
      

  20.   

    sorry 公司打扫卫生,耽误的一会
    你说的对,我刚才看错了,第一个class_temp,set nothing 后,collection_temp还可引用。谢谢‘葡萄’.
    vb 版比其他版,好多了。有这么多热情的兄弟帮忙。希望各位,团结一心,继续努力,共同提高!!!!!!!!