Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Array Re-Dimension Outside of Scope


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Mar 24, 2016 9:58 AM Last Post By: Andrew McIsaac
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 23, 2016 12:24 PM
Good afternoon,
This project Windows, XE3 C++

Looked through forums, have not found anything similar, sorry if this is incorrect Forum Group.

Reading a table, calculating values, then displaying.
When filtering this data - instead of recalculating and having user wait, it would be better to pull data once, put into memory, and then only display what is required after - filters can easily be rolled back ...
Problem I am having is to Create an Array ( Dynamically sized ) and available through different functions.

I have created a Two Dimensional String array with findings from Google:

// goal = values_db [120][60] :: 120 to start - but this may be different as number of rows in this table will change ...

int r_max = 120; String **values_db;
values_db = new String* [r_max];
for (int row=0;row<r_max;row++)
{
values_db[row]= new String[60];
}

If this is within a function - Button1, and can populate data,

Then array data can not be seen by Button2 ... out of scope...

If I try to create this as a global - I can not use other than a const to Dimension...

Does this need to be in a Class or Struct somehow ?

Thanks in Advance,

Andrew
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 23, 2016 12:42 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Andrew wrote:

Problem I am having is to Create an Array ( Dynamically sized ) and
available through different functions.

You are declaring your array as a local variable inside each function. You
need to declare it as a member of your TForm class instead.

I have created a Two Dimensional String array

You should seriously consider using a std::vector instead of raw pointers:

#include <vector>
 
class TMyForm : public TForm
{
...
private:
    std::vector<std::vector<String> > values_db;
...
};
 
...
 
int r_max = 120;
values_db.resize(r_max);
for (int row = 0; row < r_max; ++row)
{
    values_db[row].resize(60);
}


--
Remy Lebeau (TeamB)
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 23, 2016 12:52 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
thank you for replying.

I added the Include, and the Class

Error now is still doesn't understand variable values_db on line :

values_db.resize(r_max);

Thanks,
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2016 8:07 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you to Remy Lebeau (TeamB)
Pointed me in the right direction.

This is what I have found so far which appears to be working well :
Can now resize a two Dim array... and is available Globally ...

#include <vector>

std::vector<String> myvector;

...

void __fastcall TForm_Home::Button_Resize_8x10Click(TObject *Sender)
{
int r = 8;
int c= 10;
value_db.resize(r,c);
value_db[0,0]="Eight by Ten";
}

void __fastcall TForm_Home::Button_Resize_3x3Click(TObject *Sender)
{
int r = 3;
int c= 3;
value_db.resize(r,c);
value_db[0,0]="Three by Three";

}

void __fastcall TForm_Home::Button_Show_ValueClick(TObject *Sender)
{
ShowMessage( value_db[0,0]);
}

Thanks,
Andrew
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2016 9:58 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Typo :

s/b value_db not myvector on :
std::vector<String> myvector;

Thanks,
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Array Re-Dimension Outside of Scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2016 9:22 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Thought it was there, but can not get different values, when putting data into the matrix, all are the same, more work required.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02