一类怪异 [英] array of class weirdness

查看:88
本文介绍了一类怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 研究员VBA,

 Fellow VBAers,

我创建了一个类,一个类的对象和一个类的数组。 
在迭代中,我加载类对象,然后将其分配给迭代变量下标的类数组。 
当每个类对象添加到类数组中时,我打印其内容以进行验证。 
一切都没问题。

I create a class, an object of the class and an array of the class.  In an iteration I load the class object and then assign it to the class array subscripted by the iteration variable.  As each class object is added to the class array, I print its contents for verification.  Everything’s ok.

 

迭代后,我打印类数组。 
此时所有数组项都具有相同的值–添加到数组中的最后一个类对象的内容。

After the iteration, I print the class array.  At this time all array items have the same value – the contents of the last class object added to the array.

 

我很难过。 
任何帮助将不胜感激。

I am stumped.  Any help will be greatly appreciated.

 

这里’代码:

 

选项比较数据库

选项明确

 

Public Function test()

   
Dim i As Double

    Dim i As Double

   
Dim fieldSpecArray()As HerbariumFieldSpecification

    Dim fieldSpecArray() As HerbariumFieldSpecification

   
Dim fieldSpec As New HerbariumFieldSpecification

    Dim fieldSpec As New HerbariumFieldSpecification

   

   
ReDim fieldSpecArray(3)

    ReDim fieldSpecArray(3)

   
设置fieldSpec = New HerbariumFieldSpecification

    Set fieldSpec = New HerbariumFieldSpecification

 

   
对于i = 0到2

    For i = 0 To 2

       
fieldSpec.hname = Str(i)

        fieldSpec.hname = Str(i)

       
Debug.Print fieldSpec.hname'验证确定

        Debug.Print fieldSpec.hname ' verifies ok

   
   设置fieldSpecArray(i)= fieldSpec

      Set fieldSpecArray(i) = fieldSpec

       
Debug.Print fieldSpecArray(i).hname'验证确定

        Debug.Print fieldSpecArray(i).hname ' verifies ok

   
Next i

    Next i

 

   
对于i = 0到2

    For i = 0 To 2

       
Debug.Print fieldSpecArray(i).hname   '所有值相同

        Debug.Print fieldSpecArray(i).hname  ' all values same

   
Next i

    Next i

 

   
设置fieldSpec = Nothing

    Set fieldSpec = Nothing

结束函数

 

我的配置是Access 2003,Win7 / 64b。

谢谢

推荐答案

好的,现在我开始认为我中风了。收藏时也会发生同样的事情。 看看这段代码:

Ok, now I'm beginning to think I've had a stroke.  The same thing happens with collections.  Check out this code:

公共功能测试3()

    Dim c As New Collection

    Dim f As HerbariumFieldSpecification

    Dim g As HerbariumFieldSpecification

    Set f = New HerbariumFieldSpecification

  ;   f.hname =" a"

    c.Add f

    Debug.Print" ---第一次---" b $ b    For each g In c

        ; Debug.Print g.hname

    Next g
$
    f.hname =" b"

    c.Add f

    Debug.Print"---第二次---"

   对于每个g in c

        Debug.Print g.hname

   下一个g

结束函数

Public Function test3()
    Dim c As New Collection
    Dim f As HerbariumFieldSpecification
    Dim g As HerbariumFieldSpecification
    Set f = New HerbariumFieldSpecification
    f.hname = "a"
    c.Add f
    Debug.Print "---1st time---"
    For Each g In c
        Debug.Print g.hname
    Next g
    f.hname = "b"
    c.Add f
    Debug.Print "---2nd time---"
    For Each g In c
        Debug.Print g.hname
    Next g
End Function

这得到:

call test3()

- --1次--- ---
a

---第二次---

b

b

call test3()
---1st time---
a
---2nd time---
b
b

由于数组和集合在这种情况下的行为似乎相同,我必须做出一个我看不到的基本逻辑错误。 因此中风怀疑。

Since arrays and collections seem to behave the same way in this circumstance, I must be making a fundamental logic error I can't see.  Hence the stroke suspicions.

干杯,加里


这篇关于一类怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆