473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to initialize array in a structure

6
Hi;
I neeed help with the following code to initialize:
Expand|Select|Wrap|Line Numbers
  1. Structure test1
  2.  
  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"
  6.  
  7. next i
How to initialize test1 and assign some values?

Best Regards,
M.Kol
Jul 20 '12 #1
3 2598
Frinavale
9,735 Expert Mod 8TB
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
Jul 27 '12 #2
mkol
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.

Kol
Jul 29 '12 #3
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.
Example:
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
  6.  
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

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

Similar topics

6
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
1
by: MAF | last post by:
Is there a quick way to initialize an array with one value for example int IDs = new int; I want all items to be initialized to -1.
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
2
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hello, Suppose I have defined a structure.... Structure RenameDocs Public Facility As String Public Document As String Public Subtitle As String Public Page As String End Structure
1
by: dissectcode | last post by:
Hello - Please tell me what this is...It looks like an array/structure/prototype/pointer... STATIC void *_aname = { { &d0, &d1 } , { &d10, &d11 } } ; i've never seen this before please...
8
by: Roy Strachan | last post by:
I have seen this before and know it works (at least on an old Borland compiler). Unfortunately, though my memory is good, it's short. Using gcc how do I (can I?) initialize TESTSTRUCT.ssize to ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.