如何循环访问结构体?
   比如
   type a
     chi1 as string
     chi2 as string
   end type
  
   如何循环遍历他的每一个元素?  这个问题目的是想征求这么一个解决方案:
  就是提供一个对象,
  即能够直接以键值访问他的子元素,
  也能够根据index值访问他的子元素。 算法一:
  a.chi1="s"
  a.chi2="s"
  
 算法二:
  for i=0 to a.count-1
   a(i)="s"
  next
  
  各位大虾,如果能做到一个对象,算法一和算法二等价,100分赠送。

解决方案 »

  1.   

    定义数组或者集合行不行啊
    如:
    dim A(10) as a
      

  2.   

    type a
         chi(1) as string
    end type
    public abc as a算法一:
      abc.chi(0)="s"
      abc.chi(1)="s"
      
     算法二:
      for i=0 to 1
       abc.chi(i)="s"
      next
    不知是这个意思吗?
      

  3.   

    哈,楼主提问有点意思:  即能够直接以键值访问他的子元素,
      也能够根据index值访问他的子元素。但算法一又不是以键值访问,或许改成这样才对题:
    a("Chi1")="asd"
    否则,就象楼上说的,把成员归到一个数组就等价了啊!用集合 Collection 对象就可以了!
      

  4.   

    算法一:
      a.chi1="s"
      a.chi2="s"
      
     算法二:
      for i=0 to a.count-1
       a(i)="s"
      next本身就有矛盾, 不可能的
    算法1, a是对象,可以访问
    a(i)的格式变成了 a是数组, 又是对象又是数组,到底是什么?
      

  5.   

    type a
         chi(1) as string
    end type
    public abc as a这个办法行的通。
      

  6.   

    方法是可以, 不过不是楼主所要的
    如果是
    type a 
        chi(i) as string
    end typea.chi(0)= ..
    a.chi(1)=...for i...
      a.chi(i)=...
    next i跟直接一个循环有什么关系??楼主的意思是, 有个东西,他由几个东西组成, 可以调用obj.a obj.c 
    也可以调用 obj(0) obj(1) 等, 他们是相同的概念
    我认为不可能,原因如上所述 , 当然不准也有高人做出来了
      

  7.   

    类代码如下:但字段数必须事先知道(将Fields设为缺省属性)Dim s(0 To 1) As String
    Dim aCount As Integer
    Public Property Get Fields(index As Integer) As String
        If index >= 0 And index < zcount Then
            Fields = s1
        End If
    End PropertyPublic Property Let Fields(index As Integer, s As String)
        If index >= 0 And index < zcount Then
            s(index) = s
        End If
    End PropertyPublic Property Get F1() As String
        F1 = s(0)
    End PropertyPublic Property Get F2() As String
        F2 = s(1)
    End PropertyPublic Property Let F1(ss As String)
        s(0) = ss
    End PropertyPublic Property Let F2(ss As String)
        s(1) = ss
    End Property
      

  8.   

    Private Sub Class_Initialize()
        aCount = 2
    End Sub
      

  9.   

    我想楼主的意思是不是想和RecordSet的Fields那样,既可以用字段名,也可以用Index来引用呢?就是Fields("A") 等于fields(0),加入字段1名称为“A”这样的话,可以将字段放在一个集合里(colFields)Fields(Index as variant),
    dim vType as integer
    dim idx as long
    vType=varType(Index)
    if vtype=8 then' is string
      idx = colFields.Item(Index)
    elseif vtype =2 or vtype =3 or vtype=17 then'is number
      idx = index
    end if然后根据该数值取得相应的值
      

  10.   

    如果全都是 string 的话非常容易。
      

  11.   

    pigsanddogs(我爱吃猪肉,但是长不胖,为什么??)   朋友完全理解了我的意思!!!
     如果是mm,kiss一下!! ^_^
     lazycat818(lazycat818) 朋友提供了几种方法,俺去试一试,
     哈哈,还有木有哪位大虾提供其它的方法呢?
      

  12.   


     哈哈,那个在结构体里面写数组的朋友,抱歉啊,可能是我表达的意思不清楚啊,
     那种方法确实不是我想要的 ^_^
      俺要的就是 可以调用obj.a obj.c 
              也可以调用 obj(0) obj(2) 
       方法等价