Solved

Loading a multi-dimensional static array

Posted on 2004-10-30
239 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
Question by:d32coder
    7 Comments
     
    LVL 31

    Expert Comment

    by:moorhouselondon
    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
    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
    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
     

    Author Comment

    by:d32coder
    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
    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:
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…

    860 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now