Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP - class fileds accessing data and writing data

Status
Not open for further replies.

mike9606

Programmer
May 24, 2000
8
US
I'm using delphi 5 for a uni assignment I need to implement a path object which contains an array of points, the colour of the line and the width of the line. I also need to write the methods for accessing the colour and witdh and array of points and also methods to set the values of each.<br><br>I'm new to delphi but have muddle my way through some basics this week but i am really stuck with creating a class with properties and accessing those properties, i've been using the var section to hold the above variables and can write methods to access them but when trying to tie it all into the class declaration it all falls apart and i get access violation errors when i click on the form (it compiles fine though).<br><br>I'd really appreciate any help i can get on this as my deadline is getting close and i seem to be going round in circles and not going anywhere as all the literature i read is based on existing components not user created objects
 
Hi! I write some class(but under Delphi4).<br>You can change it for your program.<br><br>// unit<br>unit Unit2;<br><br>interface<br><br>uses Windows,Graphics;<br><br>type<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TRec =array of TPoint;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TA = class(TObject)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FPath&nbsp;&nbsp;: TRec;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FColor : TColor;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FWidth : integer;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;procedure WritePath( Path : TRec);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;constructor Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destructor Destroy; override;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property Path:TRec read FPath write WritePath;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property Color:TColor read FColor&nbsp;&nbsp;write FColor;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property Width:integer read FWidth write FWidth;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;procedure WritePoint( No : integer; Pnt : TPoint);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>implementation<br><br>procedure TA.WritePath( Path : TRec ); // destroy old and writes new path<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetLength(FPath,0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FPath:=Path;<br>end;<br><br>procedure TA.WritePoint( No : integer; Pnt : TPoint);// allocates memory until<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// new point and write value of pnt<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if Length(FPath)&lt;=No then SetLength(FPath,No+1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FPath[No]:=Pnt;<br>end;<br><br>constructor TA.Create;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inherited;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FPath:=nil;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FColor:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FWidth:=0;<br>end;<br><br>destructor TA.Destroy;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetLength(FPath,0); // dispose path<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inherited;<br>end;<br>end.<br>// end of unit<br><br>in program:<br><br>Uses Unit2;<br><br>procedure SomeProc;<br>var a:TA;<br>&nbsp;&nbsp;&nbsp;&nbsp;i : integer;<br>&nbsp;&nbsp;&nbsp;&nbsp;P:TPoint;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a:=TA.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i:=0 to 20 do a.WritePoint(i,Point(i,i));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.Color:=clRed;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.Width:=3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Label1.Caption:=IntToStr(a.Path[5].x);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.Free;<br>end;<br>
 
thanks alot vladmir the code was most helpful<br><br>I had manged to do most of it the major thing i was missing was the object.create and thats why i was getting the memory violations.<br><br>my next problem is when setting the line width, if i use a large width and then try and display a smaller with the larger width image is stil lin the background so the change doesn't appear to happen. I've looked at refresh and visible options but i can't seem to get the old line image to dissapear before i update to the new line image.<br><br>how do i refresh the form or image so tha the old line dissapears but the line array of tpoint is still intact so i can redraw the line with it's new properties
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top