Updating structure elements

Posted on 2006-05-14
Last Modified: 2010-05-01
I have a program the uses the following structure:

Private Type tagUse
  tagname As String
  use1 As String
  use2 As String
  use3 As String
  use4 As String
  use5 As String
  use6 As String
  use7 As String
  use8 As String
  use9 As String
  use10 As String
End Type
Dim arrTagUse() As tagUse

After a while, various array items will have different values for the various .use% elements. And for that matter, different numbers of .use% elements. I have a code section that updates the .use% correctly. It looks like
  If tagItem.use1 <= " " Then
    tagItem.use1 = usedIn
  End If
  If tagItem.use2 <= " " Then
    tagItem.use2 = usedIn
    Exit Sub
  End If

My question: is there a more elegant solution? Some way of stepping through the elements in a For - Next loop?

Question by:holyfeld
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    I don't believe there is a way to enumerate structure fields in VB6, though it is possible in VB.Net.

    If you made it a CLASS though and made the fields Public, then you can build a string which contained "use1", "use2", "use3", etc in a loop, and then would be able to get/set those values using that string and the CallByName function.
    LVL 85

    Accepted Solution

    Hi Holyfeld,
    You could simply redefine your structure as:
    Private Type tagUse
      tagname As String
      use(1 To 10) As String
    End Type

    so you end up assigning the values via something like: arrtaguse(1).use(3) = "Use item 3". You can then iterate as necessary through all the items in use() with a simple counter.



    Author Comment

    Idle_Mind answered the question I asked, so I awarded some points. rorya showed with a minor modifcation I can iterate like I want to.

    Thank you.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now