Link to home
Start Free TrialLog in
Avatar of fhtong
fhtong

asked on

Getting const array value from another unit

I have prepared a unit:

Unit1
.
type
  set01  = integer;
const
  caseset01: array[1..10] of set01 = (1,2,3,4,5,6,7,8,9,10);
...
 
and other units in my program will use this array.

I need experts to guide me how to make it done.
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

You need to write the Unit1 into the uses clause of other units where you want to use this array:

unit Unit2;

//........

uses
  ...., Unit1;
There is two possible places for the uses clause:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls {, Unit1} ;

type

//........

implementation

{$R *.dfm}

uses
  Unit1;
Avatar of fhtong
fhtong

ASKER

Thanks, esoftbg.

But how to declare in unit2, because I may have several const array and I need to find the code of how to get either one of these const array.

fhtong


You should not declare above constant (it is already declared into Unit1) - you should use it in some procedures in Unit2 ....
A simple example:



unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls;

type

//........

implementation

{$R *.dfm}

uses
  Unit1;

procedure Form2.Button1Click(Sender: TObject);
var
  I:         Integer;
begin
  I := caseset01[2] * 16;
  ShowMessage(IntToSTr(I));
end;
Do you need a full example ?
You should use the constant arrays by it's names

   caseset01[2] => you use the second element of caseset01 constant array
   caseset03[5] => you use the fift element of caseset03 constant array (if it is declared into the interface area of Unit1)

There is not need to use this type declaration:

type
  set01  = integer; (you may successfully use the Integer as type without declare set01 as a new type)

//.............................

may be you need this:

type
  TArr_1_10  = array[1..10] of Integer;

const
  caseset01:  TArr_1_10 = (01,02,03,04,05,06,07,08,09,10);
  caseset02:  TArr_1_10 = (21,22,23,24,25,26,27,28,29,30);
  caseset03:  TArr_1_10 = (31,32,33,34,35,36,37,38,39,40);
Avatar of fhtong

ASKER

If I want to have an array for unit2, can I use

Unit2
.
.
procedure Form2.Button1Click(Sender: TObject);
var
  I:         Integer;
begin
  if a then
  begin
  for I := 1 to 10 do
  workarray[I]:=caseset01[I] ;
  end else
  if b then
  begin
  for I := 1 to 10 do
  workarray[I]:=caseset02[I] ;
  end;
end;

ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you need a full working example, I could do it for you ....
Oooops, it should be:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls,
  Unit1;

var
  A:         Boolean;
  B:         Boolean;
  WorkArray:  TArr_1_10;
Avatar of fhtong

ASKER

Thanks, esoftbq

Though I may be able to solve with your advice, if you have time to give me a full working example, I would be happy to see.

fhtong
fhtong,
Please download an example application from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21486195.zip        Getting const array value from another unit
Avatar of fhtong

ASKER

Thanks, esoftbq.

I think that my solution can be solved.

fhtong

fhtong, you are welcome !
Take a look at right side of the Form2 - Button 'Work Array' - this is exactly your question.
It assignes into a WorkArray values depending on A & B variables ....