Board index » delphi » How to assign values to var ?

How to assign values to var ?

jh...@temasek.teleview.com.sg (Toh Jia Hsiang) wrote:

Quote
>I seem to have a slight problem here. I am unable to assign a value to
>a var in another unit. For example,there is this unit1 and unit2. How do
>I assign a value to a var in unit2 when control is with form1. When I
>tried to display the value in the var from form2, it is empty.

        Not sure exactly what you're doing. You know you can reference variables
from Unit1 in Unit2 by saying Unit1.VariableName, right?

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

 

Re:How to assign values to var ?


Toh and others,

Quote
Toh Jia Hsiang wrote:

> I seem to have a slight problem here. I am unable to assign a value to
> a var in another unit. For example,there is this unit1 and unit2. How do
> I assign a value to a var in unit2 when control is with form1. When I
> tried to display the value in the var from form2, it is empty.

Easy... say you have two units (UNIT1.PAS and UNIT2.PAS).  There is a var
called "MyVar : String" in UNIT1.PAS, and in UNIT2.PAS you want to change
MyVar to the value "Hello".  Do it like this :

unit unit2

..
uses <delphi inserted unit names>, unit1;
..
begin
     Unit1.MyVar := 'Hello'
end.
..
end.

Kind Regards
Luke Kenny

--
============================================
Luke Kenny - l...@netspace.net.au
Melbourne, Australia - Phone +61-3-9802-0869
============================================

Re:How to assign values to var ?


jh...@temasek.teleview.com.sg (Toh Jia Hsiang) wrote:

Quote
>I seem to have a slight problem here. I am unable to assign a value to
>a var in another unit. For example,there is this unit1 and unit2. How do
>I assign a value to a var in unit2 when control is with form1. When I
>tried to display the value in the var from form2, it is empty.

Make sure the variable you are trying to access is declared
in the INTERFACE section of the unit.  This will allow you to
access it form other unit.  Then, make sure you have that unit in the
USES clause of the other unit.  Eg:

Unit FirstUnit;
Interface
Var  MyVariable : string;
      .
      .
      .
End. {FirstUnit}

Unit SecondUnit;
Interface
Implementation

Uses FirstUnit;

Procedure Test;
Begin
   MyVariable := 'This string was set in SecondUnit';
End;

End. {SecondUnit}

Also, to avoid confusion, make sure you don't have a variable of the
same name in SecondUnit.

Godd luck!

Warren Sande
EcoTech Research
Regina, SK
ecot...@unibase.unibase.com
Voice: (306) 352-2468

Other Threads