?
Solved

I feel stupid asking this, but here it goes...

Posted on 2003-03-25
13
Medium Priority
?
247 Views
Last Modified: 2010-04-04
Okay i'm starting to feel bad now... constantly asking such "newbie" questions on "experts-exchange" but oh well, it's hard to find good delphi resources. I have books but they dont explain everything. Anyways, here is my next oh so hard question! =) Get ready to rack up the points guys!

Loading an array

Okay of coarse it's really easy to load your basic array like

Const
  MyArray: Array [0..1] of Integer;

Begin
  MyArray[0]:= 10;
  MyArray[1]:= 20;
  ...etc
End;

But is there a way (i'm doubting it, but you never know) to load a larger array easier, for example, lets say I have an array like

Const
  MyArray: [0..49] of String;

Begin
  MyArray[0] := 'Here is a string';
  MyArray[1] := 'Wow another string';
  MyArray[2] := 'This is getting tedious';
  ...etc  

  Is there a way to do something like (of coarse this doesnt compile, but I think it will show what I am trying to do)

  MyArray := ('Here is a string', 'Wow another string', 'This is not as tedious if I could do this');

Like I said, of coarse that doesnt compile, but what i'm trying to do is load an array of strings without havint to do the MyArray[x] := 'some string'; but load them all with fewer lines of code.

This question is kinda weird so if you guys don't know what i'm trying to ask, then I will be glad to try and explain this even better. Darn i wish I had friends that used Delphi... everyone is into VB! The books I have don't say anything about doing something like this, so I am kind of assuming I will have to do it the longer way. I would offer more points, but i'm running a bit low right now... sorry

Best Regards, again
Steve
0
Comment
Question by:PoeticAudio
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 6

Author Comment

by:PoeticAudio
ID: 8208211
Oops just noticed a little syntax error on that little code example...

it was supposed to be...

Const
  MyArray: Array [0..49] of String;


Sorry, just saving a little dignity here =)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8208223
Const
 MyArray: [0..49] of String = ('Here is a string', 'Wow another string', 'This is not as tedious if I could do this',...,'Last Entry');

... must be replaced with the missed elements

meikl ;-)



0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8208227
oops
MyArray: array[0..49] of String = ('Here is a string', 'Wow another string', 'This is not as tedious if I could do this',...,'Last Entry');

array-keyword was missed ;-)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
LVL 7

Expert Comment

by:billious
ID: 8208245
...but you just asked effectively the same question in
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20562544.html

...Bill
0
 
LVL 6

Author Comment

by:PoeticAudio
ID: 8208306
No, I wasnt clear, I wanted to not do it in my constants sections, I want an array that can change depending on the procedure...

Var
  MyArray: Array [0..2] of string;

Procedure SomeProcedure;
Begin
  MyArray := ('a string', 'another string', 'Another  
   string');
End;

Procedure SomeOtherProcedure;
Begin
  MyArray := ('Diff string', 'another diff string', 'yet  another diff string');
End;

Of coarse that doesnt compile, I would have to go the long way like

Procedure SomeProcedure
 MyArray[0] := 'A String';
 MyArray[1] := 'Another diff string';
...etc

I know that I can load an array in the constants section, but then I cant change the contents of that array, since its constant. In other words, I am writing a program and 10 different procedures are using the same array but each of the 10 procedures loads something different into that array. The array in this program loads 50 strings so its extremely tedious to go

  MyArray[1] := 'string 1';
  MyArray[2] := 'String 2';
  MyArray[3] := 'String 3';

It would be very nice if in each procedure that loads different strings into this 1 array could go more like

  MyArray := ('String 1', 'String 2', 'String 3');

I am just wondering if there is a way to do something like this.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8208410
>MyArray := ('String 1', 'String 2', 'String 3');

this is not possible
0
 
LVL 6

Author Comment

by:PoeticAudio
ID: 8208422
Yeah I know that exact syntax isnt possible, so the 1 and ONLY possible way to load an array is by using every array index like

MyArray[0] := whatever;
MyArray[1] := whatever;
....
MyArray[50] := whatever; ???

darn, that stinks I have lots of tedious programming ahead of me! =) oh well. Guess that's part of programming.
0
 
LVL 7

Accepted Solution

by:
billious earned 80 total points
ID: 8208426
type
 ar2 = Array [0..2] of string;

var
 myarray : ar2;

Procedure SomeProcedure;
const
  p1
Begin
 MyArray := ('a string', 'another string', 'Another  
  string');
End;

Procedure SomeOtherProcedure;
Begin
 MyArray := ('Diff string', 'another diff string', 'yet  another diff string');
End;

 

Var
 MyArray: Array [0..2] of string;

Procedure SomeProcedure;
Begin
 MyArray := ('a string', 'another string', 'Another  
  string');
End;

Procedure SomeOtherProcedure;
Begin
 MyArray := ('Diff string', 'another diff string', 'yet  another diff string');
End;

0
 
LVL 7

Expert Comment

by:billious
ID: 8208427
type
 ar2 = Array [0..2] of string;

var
 myarray : ar2;

Procedure SomeProcedure;
const
  p1
Begin
 MyArray := ('a string', 'another string', 'Another  
  string');
End;

Procedure SomeOtherProcedure;
Begin
 MyArray := ('Diff string', 'another diff string', 'yet  another diff string');
End;

 

Var
 MyArray: Array [0..2] of string;

Procedure SomeProcedure;
Begin
 MyArray := ('a string', 'another string', 'Another  
  string');
End;

Procedure SomeOtherProcedure;
Begin
 MyArray := ('Diff string', 'another diff string', 'yet  another diff string');
End;

0
 
LVL 7

Expert Comment

by:billious
ID: 8208444
Kindly ignore previous - XP trouble!




type
 ar2 = Array [0..2] of string;

var
 myarray : ar2;

Procedure SomeProcedure;
const
 p1 : ar2 = ('a string', 'another string', 'Another  
 string');
Begin
 MyArray := p1;
End;

Procedure SomeOtherProcedure;
const
 p2 : ar2 = ('Diff string', 'another diff string', 'yet  another diff string');
begin
 myarray := p2;
End;



...Bill
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8208528
yep, bill, this would be a solution,
better than mine:

unit init_an_array_comfortable_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure PrintOutArray;
  public
    { Public declarations }
  end;

  //you must type the array
  TMyArray = Array[0..2] of String;

var
  Form1: TForm1;
  MyArray : TMyArray; //and use this type

implementation

{$R *.dfm}

//Just for display content
procedure TForm1.PrintOutArray;
var i : integer;
begin
  for i := 0 to 2 do
    memo1.lines.add(MyArray[i]);
end;

//Helper function to init array with new values
function fillArray(AVariantArray : Variant) : TMyArray;
var
  i, ia, LowBound, HighBound : Integer;
begin
  LowBound := Low(Result);
  HighBound := High(Result);
  ia := LowBound;
  If VarIsArray(AVariantArray) then
    for I := VarArrayLowBound(AVariantArray,1) to VarArrayHighBound(AvariantArray,1) do
      if ia <= HighBound then
      begin
        Result[ia] := AVariantArray[i];
        inc(ia);
      end else break;  //TMyArray is too small for additional entries, just end filling
end;

//now the array could be filled in this way
procedure TForm1.Button1Click(Sender: TObject);
begin
  MyArray := FillArray(VarArrayOf(['123','456','789']));
  PrintOutArray;
end;

//other sample
procedure TForm1.Button2Click(Sender: TObject);
begin
  MyArray := FillArray(VarArrayOf(['abc','def','ghi','jkl']));  //last member will ignored
  PrintOutArray;
end;

end.

meikl ;-)
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 8211380
Just a thought, but if the arrays are going to be constant and pre-determined then why not just store the strings in a txt files and load them into a TStringList or TStrings object as necessary?
0
 
LVL 6

Author Comment

by:PoeticAudio
ID: 8213986
Wow! NICE! both you guys had some very nice coding, but due to ease of use I will award billious with the 20 pts. Sorry I couldnt offer more, man but I am running low on pts here. Thanks a lot =)

Best Regards,
Steve
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

601 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