?
Solved

how to initialize array

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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

741 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