Jump to content


Photo

[TNxNumberEdit] Suggestion


  • Please log in to reply
7 replies to this topic

#1 bertrod

bertrod
  • Members
  • 101 posts
  • Gender:Male
  • Location:Switzerland

Posted 30 May 2008 - 06:11 PM

Hello,

In :

TNxCustomNumberEdit.GetValue(), there is a StrToFloat() and I had problems if the user enters very large numbers (overflow exception).

I suggest you put a try-except around the StrToFloat().

What do you think ?

Regards

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,289 posts
  • Gender:Male

Posted 30 May 2008 - 08:11 PM

Hello Bertrod,

I am not sure that I understand, can you please tell me where you have locate this line (or send me one small example) and I will add it.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 02 June 2008 - 11:20 AM

Hi,

There is also TryStrToFloat with built-in exception handling.

QUOTE (bertrod @ May 30 2008, 07:11 PM) <{POST_SNAPBACK}>
Hello,

In :

TNxCustomNumberEdit.GetValue(), there is a StrToFloat() and I had problems if the user enters very large numbers (overflow exception).

I suggest you put a try-except around the StrToFloat().

What do you think ?

Regards

G.W. van der Vegt

#4 bertrod

bertrod
  • Members
  • 101 posts
  • Gender:Male
  • Location:Switzerland

Posted 02 June 2008 - 12:05 PM

Here is my TNxCustomNumberEdit.GetValue function in NxEdit.pas with the StrToFloat :

CODE
function TNxCustomNumberEdit.GetValue: Double;
var
  ValidText: string;
begin
  if Text = '' then Result := 0 else
  begin
    ValidText := GetValidText(Text, eoAllowFloat in FOptions, eoAllowSigns in FOptions);
    if ValidText = '' then ValidText := '0';
    Result := StrToFloat(ValidText);
    CheckBounds(Result);
  end;
end;


Sorry if this line has already been corrected in a more recent version. As wvd_vegt says, using TryStrToFloat should be already enough.

Regards

#5 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,289 posts
  • Gender:Male

Posted 02 June 2008 - 03:10 PM

Hello Bertrod,

Please try with next update:

CODE
function TNxCustomNumberEdit.GetValue: Double;
var
  ValidText: string;
begin
  if Text = '' then Result := FMin else
  begin
    ValidText := GetValidText(Text, eoAllowFloat in FOptions, eoAllowSigns in FOptions);
    if ValidText = '' then ValidText := FloatToStr(Min);
    TryStrToFloat(ValidText, Result);
    AdjustToRange(Result);
  end;
end;


Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#6 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 03 June 2008 - 02:41 PM

Hi Boki,

According to the help files, TryStrToFloat nothing is said about the value parameter if the function returns false.

Technically you should code something like:

CODE
if TryStrToFloat(ValidText, Result) then
  AdjustToRange(Result)
else
  result:=Min; //or some other value, probably the old/previous value


QUOTE (Boki (Berg) @ Jun 2 2008, 04:10 PM) <{POST_SNAPBACK}>
[code=auto:0]
ValidText := GetValidText(Text, eoAllowFloat in FOptions, eoAllowSigns in FOptions);
if ValidText = '' then ValidText := FloatToStr(Min);
TryStrToFloat(ValidText, Result);
AdjustToRange(Result);

G.W. van der Vegt

#7 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,289 posts
  • Gender:Male

Posted 03 June 2008 - 03:47 PM

Hello Wim,

Maybe this could be final code:

CODE
function TNxCustomNumberEdit.GetValue: Double;
var
  ValidText: string;
begin
  if Text = '' then Result := FMin else
  begin
    ValidText := GetValidText(Text, eoAllowFloat in FOptions, eoAllowSigns in FOptions);
    if ValidText = '' then ValidText := FloatToStr(Min);
    if TryStrToFloat(ValidText, Result) then
    begin
      AdjustToRange(Result);
      FValue := Result;
    end else Result := FValue;
  end;
end;


Previously declare FValue in private section and set

CODE
FValue := 0;


inside Create of TNxCustomNumberEdit

Best regards and thanks
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#8 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 05 June 2008 - 10:56 AM

Hi Boki,

Looks ok to me.
G.W. van der Vegt




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users