Solved

how to initialize array

Posted on 1998-07-18
3
205 Views
Last Modified: 2010-04-04
Hello.

I want to know how to initialize array.

(ex)
type
TForm1 = class(TForm)
..
private
..
public
..
end;

var
  arrayA: array[0..2] of Integer := (0,1,2);

It's Ok.But when array is class member, compile error occurs.

type
TForm1 = class(TForm)
..
private
m_arrayA: array[0..2] of Integer := (0,1,2);
..
public
..
end;

So I have to program as follows.

type
TForm1 = class(TForm)
..
private
m_arrayA: array[0..2] of Integer;
..
public
..
end;
..
procedure TForm1.FormCreate;
begin
m_arrayA[0] := 0;
m_arrayA[1] := 1;
m_arrayA[2] := 2;
end;

This code is very ugly.
I want to initialize array like C or Java.

Please teach me a good method.

Sakya
0
Comment
Question by:sakya
[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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1358149
Here's an approach, a bit less ugly, or...?

type
  TMyIntArray = array[ 0..3 ] of Integer;
  TForm = class(TForm)
  ...
  private
    m_arrayA: TMyIntArray; // prevent "Incompatible types" error
  ...
  end;

var (or const, whatever you prefer...)
  rgiIntermediate: TMyIntArray = ( 0, 1, 2, 3 );

procedure TForm1.FormCreate;
begin
  m_arrayA := rgiIntermediate;
end;


This really doesn't shorten your code, but looks a bit better.

/// John
0
 

Author Comment

by:sakya
ID: 1358150
Thanks for your answer.
But is this  Delphi's limit???

Sakya


0
 
LVL 4

Expert Comment

by:erajoj
ID: 1358151
I think so.
Not even C++ can initialize member vars in the class descr. It has to be done in the constructor or later (unless static-decl?), but you probably already knew that?
Object Pascal's strong typing is sometimes a pain in .... This has other great advantages however.
If you are initializing huge structures; use datafiles, the registry or load the data from your resources as binary data. If you want to do so, and don't know how, ask again...

Regards,
/// John
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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