Board index » delphi » Declare variables at runtime??

Declare variables at runtime??

Hi Delphi-Folks

Is it possibly to declare some variables
at runtime? (while the program is running??)..

Please give me an example...

Thanks for your help...

Greetings from Roli, Switzerland    chip.informa...@bluewin.ch

 

Re:Declare variables at runtime??


No.
Cannot be done.
The violates the entire concept of a compiled language.

Rick

Re:Declare variables at runtime??


Quote
Rick Hall wrote:
> No.
> Cannot be done.
> The violates the entire concept of a compiled language.

> Rick

Had to go back and find the original question (I always wonder about
"can't be done" messages):

Quote
>> Hi Delphi-Folks

This is NOT a Delphi group, though this should apply to Delphi.

Quote
>> Is it possibly to declare some variables
>> at runtime? (while the program is running??)..

I'm not sure how to read this question.  Do you really mean declared, or
do you mean assigned (as in, declaring the value of the variable)?  All
variables that are going to be used would have to be contained within
the source; they don't just fall out of the sky when the program
begins.  However, variables usually have an undefined value at runtime.

If you do indeed mean the latter, you can use a typed constant (which
isn't actually a constant), such as:

program constdemo;

const
  typed : string = 'This is a message.';

var
  undefined : string;

begin
  writeln (typed); {This should print "This is a message."}
  writeln (undefined); {This should print garbage.}
end.

Quote
>> Please give me an example...

That help?  If not, perhaps you can clarify the question?

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:Declare variables at runtime??


Quote
Scott Earnest wrote:
> Rick Hall wrote:

> > No.
> > Cannot be done.
> > The violates the entire concept of a compiled language.

> Had to go back and find the original question (I always wonder about
> "can't be done" messages):

Agreed. It bugs me when programmers say something can't be done, whether
or not it's true.

Quote
> >> Is it possibly to declare some variables
> >> at runtime? (while the program is running??)..

> I'm not sure how to read this question.  Do you really mean declared,
> or do you mean assigned (as in, declaring the value of the variable)?
> All variables that are going to be used would have to be contained
> within the source; they don't just fall out of the sky when the
> program begins.  However, variables usually have an undefined value at
> runtime.

I read the question differently than that. Kinda thought he meant
something along the lines of allocation of variables at runtime. More
accurately stated as dynamic memory allocation. This, of course, is
possible with pointers.

While I'm sure those who have replied are not novices, I will give a
very brief example for the benefit of the original poster.

  Program PointerSample;
  Type
    MyType = Integer;
  Var
    P: ^MyType;
  Begin
  WriteLn('Heap memory available: ',MemAvail);
  WriteLn('Allocating memory for pointer...');
  New(P);
  WriteLn('Heap memory available: ',MemAvail);
  WriteLn('Disposing of pointer...');
  Dispose(P);
  WriteLn('Heap memory available: ',MemAvail);
  End.

To be sure, this is about as simple an example as one can get, and
therefore not terribly useful in itself. But it demonstrates the
essentials of New and Dispose. A pointer to an integer might not be too
exciting, but consider the possibilities if the pointer pointed to a
string or to a record (which might contain other pointers).

Well, that should get you started. Check the online help for New to see
another example using a string.

AME

Re:Declare variables at runtime??


I think you want at runtime descide if a certain variable is for instance a
string or a longint. Try this :

var s : string;
     l : longint absolute s;

begin
  s := 'Hello';
  writeln(s); {this should write 'Hello' to your screen}
  l := 12565423;
  writeln(l); {this should write 12565423 to your screen}
  writeln(s); {this should write garbage to your screen}
end.

Roland Hartmann <chip.informa...@bluewin.ch> wrote in article
<33A0E128.3...@bluewin.ch>...

Quote
> Hi Delphi-Folks

> Is it possibly to declare some variables
> at runtime? (while the program is running??)..

> Please give me an example...

> Thanks for your help...

> Greetings from Roli, Switzerland    chip.informa...@bluewin.ch

Re:Declare variables at runtime??


Re:Declare variables at runtime??


Hi,

I am writing a windows HTML site manipulation tool and would be interested
in anyone who would like to have a look at it and give me some feedback.

Currently the program has the following functions;

main screen

Quote
> Loads/saves and size (limited by memory - no 32k limit!) HTML files and

also any linked files it can find
Quote
> Shows drop down view of each page with links/resources/anchors/bad links
etc
> Global search/replace across entire loaded pages as required
> graphical representation of altered files (will save only altered ones if

required)

HTML code view with strange syntax highlighting

Quote
> cursor movement currently

full graphical interface
  > custom dialogs

Quote
> custom button bar (similar to IE3-4 style)
> very pretty (nice icons etc)

current program size about 140k (yes that is correct)

If you are interested (or just have suggestions, or additions) then please
drop me a line (allow time for me to reply)

Lucifer

Other Threads