How to initialize array in a structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkol
    New Member
    • Jan 2012
    • 6

    #1

    How to initialize array in a structure

    Hi;
    I neeed help with the following code to initialize:
    Code:
    Structure test1
    
    <VBFixedArray(6)> Dim Id() As string
    public sub initializetest1
      ReDim Id(6)
    end sub
    End structure 
    Dim myvalues() as test1
    '.
    '.
    ReDim myvalues(10)
    How to assign values to elements of test1?
    Code:
    dim i as short=0 to 10
    myvalues(i).Id(1)="V"
    myvalues(i).Id(2)="H"
    '.
    myvalues(i).Id(6)="J"
    
    next i
    How to initialize test1 and assign some values?

    Best Regards,
    M.Kol
    Last edited by Frinavale; Jul 27 '12, 02:04 PM. Reason: Fixed code tags so that it is not encompassing the entire post.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Array indexes start at 0.
    Your array has has 7 elements available to it.
    You can set elements in the array from indices 0 to 6 (7 elements).

    Is there a problem with your code (other than you aren't using the first space available in your array)?

    Have you considered creating a constructor for your array that takes a string that is 7 characters long...and in that constructor, initializing the array?

    -Frinny

    Comment

    • mkol
      New Member
      • Jan 2012
      • 6

      #3
      Mr. Frinny,
      Thank you for your answer. I have no problem with the code. I know I can set elements in the array from indices 0 to 6(see above posting). I just want to know how to initialize test1 and assign some values, creating a constructor is fine with me -as long as it works -just show me the code so I know what you are talking about.

      Kol
      Last edited by Niheel; Jul 29 '12, 12:58 AM.

      Comment

      • kiseitai2
        New Member
        • Jul 2007
        • 93

        #4
        Do you see the initializetest1 sub in your code? 1) You could add a for loop below ReDim and cycle through the array while adding a random letter of your choosing. 2) You could do what Frivanale says. 3) You could create a function/sub that does #1.
        Now, I do not know whether a VB 6 constructor works like a C++ constructor (i.e.

        VB -> test1(ByVal initString as String)...,

        C++ ->
        Code:
        test1(string initString){
          char c[] = initString.c_str(); 
          for(int i=0; i<initString.size();i++)
          {id[i]=c[i];}
        }
        ).

        #3, however, is the easiest way. Just write a sub or function that does #1 or #2.
        Example:
        Code:
        Sub A (ByVal b as String)
          For c as integer =0 to b.size()'can't remember
            id(c)=b.char(c)
          Next c
        End Sub
        I probably made some mistakes since I have been coding in C++ recently, so I am a bit rusty on VB. However, if any of the options I gave you is valid and usable, I want you to ask me how did I tackled the problem and found a diverse set of answers. I really want to make sure you are learning from your question and not just simply copying and pasting. After all, the future is in bits and understanding programming now will be invaluable to you! :)
        Last edited by kiseitai2; Jul 30 '12, 06:12 PM. Reason: Changed Dim for ByVal, since that's the keyword that starts an argument statement in a function or sub.

        Comment

        Working...