Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Classes, function cant assign value to String

i get and error in Load From String, the SubStr function returns a good String value, but i cant assign it to pNumber, bfore it assign its i can read pNumber fine, but after it says the value is innacessible ..

thanks for help .

unit UnitUtils;

interface
  uses SysUtils ;
         
  function CharIndex(const Str : String ; Chr : Char ; From : Integer = 0) : Integer ;
  function SubStr(const Str : String ; From, ToPos : Integer) : String ;

implementation

  function CharIndex(const Str : String ; Chr : Char ; From : Integer = 0) : Integer ;
  var
    i : Integer ;
  begin
    CharIndex := -1 ;
    for i := From to Length(Str) do
      if Str[i] = Chr then
      begin
        CharIndex := i ;
        Exit ;
      end ;
  end ;

  function SubStr(const Str : String ; From, ToPos : Integer) : String ;
  var
    RString : String ;
    i : Integer ;
  begin
    RString := '' ;
    if (ToPos > Length(Str)) or (ToPos < From) then
      ToPos := Length(Str) ;
    for i := From to ToPos-1 do
      RString := RString + Str[i] ;
    SubStr := RString ;
  end ;
end.

------------

unit UnitProduct;

interface
  uses SysUtils, UnitUtils ;

const
  ProductNumberLength = 15 ;
  ProductNameLength = 45 ;
  ProductWebLength = 255 ;

type
  TTaxes = (tNone, tFederal, tProvincial, tBoth) ;
type
  TProduct = class (TObject)
  private
          pNumber : String ;
          pName : String ;
          pWeb : String ;
          pPrice : Real ;
          pQuantity : Word ;  
          pTaxes : TTaxes ;
  published
    constructor Create(Number, Name, Web : String ; Price : Real ; Quantity : Word ; Taxes : TTaxes) ;

    function TaxesToString : String ;
    procedure StringToTaxes(Value : String) ;
    function SaveToString : String ;
    function LoadFromString(Value : String) : Boolean ;

    property Number : String read pNumber write pNumber ;
    property Name : String read pName write pName ;
    property Web : String read pWeb write pWeb ;
    property Price : Real read pPrice write pPrice ;
    property Quantity : Word read pQuantity write pQuantity ;
    property Taxes : TTaxes read pTaxes write pTaxes ;
  end ;

implementation

  constructor TProduct.Create(Number, Name, Web : String ; Price : Real ; Quantity : Word ; Taxes : TTaxes) ;
  begin
          pNumber := Number ;
          pName := Name ;
          pWeb := Web ;
          pPrice := Price ;
          pQuantity := Quantity ;
          pTaxes := Taxes ;
  end ;

  function TProduct.TaxesToString : String ;
  begin
    case pTaxes of
      tNone: TaxesToString := '--' ;
      tFederal: TaxesToString := 'F-' ;
      tProvincial: TaxesToString := '-P' ;
      tBoth: TaxesToString := 'FP' ;
    end ;
  end ;

  procedure TProduct.StringToTaxes(Value : String) ;
  begin
    if (Value[1] = 'F') and (Value[2] = 'P') then
      pTaxes := tBoth
    else if Value[1] = 'F' then
      pTaxes := tFederal
    else if Value[2] = 'P' then
      pTaxes := tProvincial
    else
      pTaxes := tNone ;
  end ;

  function TProduct.SaveToString : String ;
  begin
    SaveToString := pNumber + '|' + pName + '|' + pWeb + '|' + FormatFloat('0.00', pPrice) + '|' + IntToStr(pQuantity) + '|' + TaxesToString ;
  end ;

  function TProduct.LoadFromString(Value : String) : Boolean ;
  var
    TmpStr : String ;
  begin
    TmpStr := SubStr(Value, 1, CharIndex(Value, '|')) ;
    pNumber := TmpStr ;
    LoadFromString := True ;
  end ;

end.
0
NightAwk
Asked:
NightAwk
  • 3
  • 2
1 Solution
 
kretzschmarCommented:
first

>function CharIndex(const Str : String ; Chr : Char ; From : Integer = 0) : Integer ;
 
can be replaced with the pos-function
(maybe paired with the copy-function)

>function SubStr(const Str : String ; From, ToPos : Integer) : String ;
 
can be replaced with the copy-function

about your problem i don't know yet,
where do you get this "inaccessable value"-message

during debugging?

btw.
i would guess you do primary java-coding, or?

meikl ;-)
0
 
NightAwkAuthor Commented:
yeah durin debigging.  

and i created char index cuz strpos only returns a pchar
to the char, i want the index, and the copy function doesnt allow me to specify where to start and where to end the copy ...

what i jsut dont understand is why it wont assign the string to the pNumber variable ...
0
 
kretzschmarCommented:
well, this happens often during debugging,
because your watched value runs out of scope

best controlling may be a global-var
rather than an object instance


>why it wont assign the string to the pNumber variable ...
are you sure about this?
just add a showmessage there


//my replacements suggestions
function CharIndex(const Str : String ; Chr : Char ; From : Integer = 0) : Integer ;
var s : string;
begin
  s := copy(str,from+1,maxlongint)
  result := pos(chr,s);
end;

function SubStr(const Str : String ; From, ToPos : Integer) : String ;
var s : String
begin
  result := copy(str,from,topos-from+1);
end;

not tested

meikl ;-)

0
 
NightAwkAuthor Commented:
Well when i call SaveToString to display the result it crashes.

and right before executing "pNumber := string" i can see the pnumber value in debugging, then i press f8, and check again and it has became unacessible.

i could try to display it there, but why would it work there and not work right after in the savetostring function call.

thanks.
0
 
NightAwkAuthor Commented:
OOOOOK well i dono exactly why, but i compiled this morning and it just like worked fine ...
so i dono why it gave me that problem

well, thanks anyway
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now