0

i get

function TForm1.GetArsHedef(): String;
const
 DosyaAdi: String = 'hello';

i need

function TForm1.GetArsHedef(): String;
const
 DosyaAdi: String = edit1.text+edit2.text;

but up the code not work who can help

now thanks . etekno

lurker
  • 56,987
  • 9
  • 69
  • 103
Kaan Yazıcı
  • 27
  • 1
  • 1

3 Answers3

3

Constants have to be constant. They have to be known at compile time. You need a variable.

var
  DosyaAdi: String; 
....
DosyaAdi := edit1.text+edit2.text;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Excepted LIBRARY but received an identifier – Kaan Yazıcı Apr 30 '16 at 22:38
  • 1
    That's another problem. You have displayed an uncanny ability to produce non sequiteurs. – David Heffernan Apr 30 '16 at 23:55
  • 1
    I do not know how I have chosen you want to print the values ​​on an editor , but it does not allow const factor (edit1.text+edit2.text) I want to do as – Kaan Yazıcı May 01 '16 at 16:16
  • 1
    Are you having difficulty understand what the word "constant" means in this context? The key part of my answer is: *they have to be known at compile time*. Does that make sense to you? Have you read anything that anyone has written? – David Heffernan May 01 '16 at 16:20
3

You need to use a variable, and in your function you won't even need to declare one. Delphi automatically declares the variable Result in a function and makes it the proper type to be returned.

function TForm1.GetArsHedef(): String;
begin
  Result := Edit1.Text + Edit2.Text;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
2

You cannot do what you wish to do. Constants are converted to their actual values at compile-time, and are not dynamic. Especially since this constant is local to a procedure, just use a variable instead. The word "constant" itself explains that it's intended to be the same value "constantly".

In any case, you cannot define or declare local variables or constants while setting their default values to another variable. To read the contents of these edit controls, it must be code explicitly written in the implementation to assign it.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Although, you might want to check this out: [Delphi assign to `const`](http://stackoverflow.com/questions/4335606/delphi-assign-to-const). – lurker Apr 30 '16 at 21:45
  • I think the op wants the value assigned to `DosyaAdi` to persist when re-entering the function. Some like assignable constants. – Johan Apr 30 '16 at 21:46
  • @lurker Indeed, assigning to a constant is technically possible, but OP wishes to define a constant with a direct reference to a control. I personally don't see any reason to want to assign to a constant when you could use a variable instead. It's like buying a car just to use it as a snow sled. – Jerry Dodge Apr 30 '16 at 21:46
  • @JerryDodge you are probably right. The OP's question is a bit terse, though, so it's hard to tell what they really need. – lurker Apr 30 '16 at 21:48
  • Well, an expression with __variables__ in it is not a __constant__ expression and you need the former to initialize a constant. – Rudy Velthuis May 02 '16 at 00:30