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.
fhtongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

unit Unit2;

//........

uses
  ...., Unit1;
esoftbgCommented:
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;
fhtongAuthor Commented:
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


OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

esoftbgCommented:
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;
esoftbgCommented:
Do you need a full example ?
esoftbgCommented:
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)

esoftbgCommented:
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);
fhtongAuthor Commented:
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;

esoftbgCommented:
unit Unit1;

interface

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

const
  FIdx = 01;
  LIdx = 10;

type
  TArr_1_10  = array[FIdx..LIdx] of Integer;

const
  caseset01:  TArr_1_10 = (01,02,03,04,05,06,07,08,09,10);

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

unit Unit2;

interface

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

var
  A:         Boolean;
  workarray:  TArr_1_10;

//........

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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esoftbgCommented:
If you need a full working example, I could do it for you ....
esoftbgCommented:
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;
fhtongAuthor Commented:
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
esoftbgCommented:
fhtong,
Please download an example application from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21486195.zip        Getting const array value from another unit
fhtongAuthor Commented:
Thanks, esoftbq.

I think that my solution can be solved.

fhtong

esoftbgCommented:
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 ....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.