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
Solved

how to initialize array

Posted on 1998-07-18
3
204 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
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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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