Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loading a multi-dimensional static array

Posted on 2004-10-30
7
Medium Priority
?
241 Views
Last Modified: 2010-04-05
This feels like poorly written written code, is there a better way?
[ D6, WinXPHome ]

-----------clip------------------
  ButtonArray: [1..7,1..5] of String;



  ButtonArray[1][1] := 'some text';
  ButtonArray[1][2] := 'some text';
  ButtonArray[1][3] := 'some text';
  ButtonArray[1][4] := 'some text';
  ButtonArray[1][5] := 'some text';

  ButtonArray[2][1] := 'some text';
  ButtonArray[2][2] := 'some text';
  ButtonArray[2][3] := 'some text';
  ButtonArray[2][4] := 'some text';
  ButtonArray[2][5] := 'some text';
  ...
-----------clip------------------

 I know I can load a single dimension array like this
    ButtonArray: array [1..5] of String = ('some text','some text','some text','some text','some text');

Is there a better way to load a multi-dimensional array?


Don
0
Comment
Question by:d32coder
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12451237
const
  maxi=7;
  maxj=5;

var
  buttonarray: array[1..maxi,1..maxj] of string;



for i:=1 to maxi do
  for j:=1 to maxj do
    buttonarray[i,j]:='some text';


0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 200 total points
ID: 12451276
doh:I'm not thinking

of course you want sometext to be different ecvery time, i would use a resource file then

loadstr(i+maxi*j);

the resource file would be numbered from 1.
0
 

Author Comment

by:d32coder
ID: 12451384
I'd rather not have the data in an external file.   It's smaller code but not really what I'm looking for.   I was hoping I could put multiple strings into a single assignment as in the one dimensional array assignment in my example.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:d32coder
ID: 12451393
Now I'm not thinking, you said a resource, not a data file.  
That's probably a good idea.  
Let's see what other experts have to say.

Don
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12451503
It depends what kind of strings you have.  In a program I'm writing at the moment, I construct the bit you put into brackets

('sometext','sometext' etc)

into an Excel file and do an Include at the declaration for the initial values, so it looks like this

   ButtonArray: array [1..5] of String = ('
{$INCLUDE progdata.csv}
');

...so that's another way.

If you are writing a multi-language program then you will want all of the language info in separate files.

0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 300 total points
ID: 12451827
var
  ButtonArray: array [1..5, 1..5] of String = (
    ('some text1A','some text2A','some text3A','some text4A','some text5A'),
    ('some text1B','some text2B','some text3B','some text4B','some text5B'),
    ('some text1C','some text2C','some text3C','some text4C','some text5C'),
    ('some text1D','some text2D','some text3D','some text4D','some text5D'),
    ('some text1E','some text2E','some text3E','some text4E','some text5E')
  );

However, if you need to fill it with data from a file or other source then you have no other option than to fill it field by field. (for I... for J... ButtonArray[I,J] :=...)
If it is a file of shortstrings then it is possible to write the whole array to file as a single block of data, though. Something like:

type
  TButtonArray: array [1..5, 1..5] of shortstring;
var
  ButtonArray: TButtonArray;
  ButtonFile: file of TButtonArray;
begin
  AssignFile(ButtonFile, 'Whatever.dat');
  Reset(ButtonFile);
  Read(ButtonFile, ButtonArray);
  CloseFile(ButtonFile);
end;

but this ONLY works for simple datatypes like integers, doubles, booleans and shortstrings. Any type that is pointer-based (normal strings, PChar's, classes) cannot be written to file like this. Furthermore, this code is incompatible with Delphi 8.
0
 

Author Comment

by:d32coder
ID: 12451967
I split the points.  
Alex had the solution I was looking for; nested parentheses.  
I will use Moorhouse's idea on loading it into a resource file for this project.  
Thanks to both of you.

Don
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question