By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

How to initialize array in a structure

P: 6
I neeed help with the following code to initialize:
Expand|Select|Wrap|Line Numbers
  1. Structure test1
  3. <VBFixedArray(6)> Dim Id() As string
  4. public sub initializetest1
  5.   ReDim Id(6)
  6. end sub
  7. End structure 
  8. Dim myvalues() as test1
  9. '.
  10. '.
  11. ReDim myvalues(10)
How to assign values to elements of test1?
Expand|Select|Wrap|Line Numbers
  1. dim i as short=0 to 10
  2. myvalues(i).Id(1)="V"
  3. myvalues(i).Id(2)="H"
  4. '.
  5. myvalues(i).Id(6)="J"
  7. next i
How to initialize test1 and assign some values?

Best Regards,
Jul 20 '12 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,731
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?

Jul 27 '12 #2

P: 6
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.

Jul 29 '12 #3

P: 93
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++ ->
Expand|Select|Wrap|Line Numbers
  1. test1(string initString){
  2.   char c[] = initString.c_str(); 
  3.   for(int i=0; i<initString.size();i++)
  4.   {id[i]=c[i];}
  5. }

#3, however, is the easiest way. Just write a sub or function that does #1 or #2.
Expand|Select|Wrap|Line Numbers
  1. Sub A (ByVal b as String)
  2.   For c as integer =0 to b.size()'can't remember
  3.     id(c)=b.char(c)
  4.   Next c
  5. 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! :)
Jul 29 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.