namcit99
asked on
There is not example on document 2005
Dear advisor !
I use Delphi 2005
I want know the example about using VAL function. But there is not example about this function. On document D2005, there is no any examlpe like D6, D7
So i should download document from www.borland.com
Is not example from document D2005 ? Example about Val and str funtion
I use Delphi 2005
I want know the example about using VAL function. But there is not example about this function. On document D2005, there is no any examlpe like D6, D7
So i should download document from www.borland.com
Is not example from document D2005 ? Example about Val and str funtion
ASKER
now, I already know to use about Val function. I complaint about document D2005. I want to read example , but nothing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i am studying using Delphi. so i should use ducument so much when coding. IT is not suitablr for beginner with document D2005. because no example for function.
Firstly, both "Val" and "Str" are procedures not functions.
Secondly, they are both very "old" and both have better modern alternatives.
Really, for "Val" you would be much better off using the "StrToIntDef" or "StrToInt64Def" functions, where you could do something like this;
MyIntVar:=StrToIntDef(MyNu mberInStr, 0);
with the added bonus of not needing to worry about numeric range limits or invalid characters in "MyNumberInStr" raising exceptions.
and for "Str" you would be better off using the "Format" function, like this;
MyNumberInStr:=Format(MyFo rmatStr,My IntVar);
the "Format" function can deal with, pretty much, any kind of variable(s) in many ways, the "Format" function is very powerful and very flexible.
Cheers,
David.
Secondly, they are both very "old" and both have better modern alternatives.
Really, for "Val" you would be much better off using the "StrToIntDef" or "StrToInt64Def" functions, where you could do something like this;
MyIntVar:=StrToIntDef(MyNu
with the added bonus of not needing to worry about numeric range limits or invalid characters in "MyNumberInStr" raising exceptions.
and for "Str" you would be better off using the "Format" function, like this;
MyNumberInStr:=Format(MyFo
the "Format" function can deal with, pretty much, any kind of variable(s) in many ways, the "Format" function is very powerful and very flexible.
Cheers,
David.
...although Val has the added functionality of returning the offending character position in the case of failure, which sets it apart from StrToIntDef or StrToInt.
ASKER
I consider on your idea
uses Dialogs;
var
I, Code: Integer;
begin
{ Get text from TEdit control }
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0, mbOk);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;
Is that what you wanted?