Solved

how to initialize array

Posted on 1998-07-18
3
199 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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
Comment Utility
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
Comment Utility
Thanks for your answer.
But is this  Delphi's limit???

Sakya


0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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