?
Solved

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

Posted on 2003-03-25
13
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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