Answered by:
Populate Vector from file and use vector content in different event handler

Question
-
Hi,
I need to read some settings values from an input file, by means of an openfiledialog and assign these values to a dynamic data structure a vector for example.
Then I need to use these vectors, in another function. I want to read the comma separated file per line and do a logical test to determine in which vector to put the content on that line.
The questions are:
1.How do I make these vectors globally available in C++;I'm doing the assignment of the vectors content in a button click event handler and I do the calculation that needs to use the vector in a different button click event handler.
This is the header for the function that needs to use the vectors:
void collissionProb3(vector<vector<double> >& multidimvec,int NMAT,int NCELL,double MESHSIZE[],int MATPAP[],double DELTA[],double XT[]);
This works fine if I initialize the vectors in the same event handler where I call collissionProb3, but I need to assign the vectors from a file, then call collissionProb3
2. How does string.split(stringName,separatingChar) work in C++: in VB you can do something like a=string.split(stringName,",")(0) to get the first element of the string before the separating comma in this example.
Thanks ,
Mugu
Sunday, August 29, 2010 3:50 PM
Answers
-
You could create the vector variable as a member of a class so that all the methods including the button click handler can access it.
To split the string you can use the strtok function.
«_Superman_»
Microsoft MVP (Visual C++)- Proposed as answer by Jesse Jiang Wednesday, September 1, 2010 8:54 AM
- Marked as answer by Jesse Jiang Monday, September 6, 2010 2:13 AM
Sunday, August 29, 2010 3:57 PM -
Since you're using C++/CLI you could use the array class - array<array<double>^>^ doublearray
«_Superman_»
Microsoft MVP (Visual C++)- Marked as answer by SM_M Friday, September 10, 2010 7:36 AM
Monday, September 6, 2010 4:21 AM
All replies
-
You could create the vector variable as a member of a class so that all the methods including the button click handler can access it.
To split the string you can use the strtok function.
«_Superman_»
Microsoft MVP (Visual C++)- Proposed as answer by Jesse Jiang Wednesday, September 1, 2010 8:54 AM
- Marked as answer by Jesse Jiang Monday, September 6, 2010 2:13 AM
Sunday, August 29, 2010 3:57 PM -
Hi Mugu,
I agree with Superman's idea, and for second question, if you use CLI/C++, you can use function split of namespace System::String, on that website we can find example code.
Regards
Jesse
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Wednesday, September 1, 2010 8:54 AM -
Thanks for your replies.
I have created the class with the vectors as members.
Where should I declare an instance of this class for it to be accessible to a button click event handler that does the file opening that I then read to store content in the vectors? This is a forms application on visual studio 2005.
Thanks Jesse, I'll try get the split to get the values between the commas, I have used it in VB.NET but it looks like everything is difficult with c++.
Regards,
Mugu
Thursday, September 2, 2010 6:25 PM -
You don't need a separate class. What class are the button click handlers in? That's the class that should have the vector as a member so the button handlers can access it.
Thursday, September 2, 2010 9:04 PM -
Create the vector variable as a member of the class that represents the form.
«_Superman_»
Microsoft MVP (Visual C++)Friday, September 3, 2010 3:51 AM -
Thanks,to some extent I'm understanding what you are saying. I have added the vector as a member variable of the form class which has the open file button that will need to populate the vector.
However, I have no clue how to initialize the vector when it is a member of class managed in c++, so I get a null object reference when I push back some data to the vector.I should ideally be doing this int the initializeComponent function, but don't know how. The managed stuff is causing my grief.
Thanks,
MuguFriday, September 3, 2010 8:19 PM -
Since you're using C++/CLI you could use the array class - array<array<double>^>^ doublearray
«_Superman_»
Microsoft MVP (Visual C++)- Marked as answer by SM_M Friday, September 10, 2010 7:36 AM
Monday, September 6, 2010 4:21 AM