Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to initialize array

Posted on 1998-07-18
3
Medium Priority
?
211 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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