?
Solved

Classes, function cant assign value to String

Posted on 2003-02-19
5
Medium Priority
?
225 Views
Last Modified: 2010-04-04
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
Comment
Question by:NightAwk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7983786
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
 

Author Comment

by:NightAwk
ID: 7984954
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 7985727
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
 

Author Comment

by:NightAwk
ID: 7987446
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
 

Author Comment

by:NightAwk
ID: 7987498
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question