Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: RAD 2010 need a collection of Objects?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 7 - Last Post: Jul 6, 2016 4:53 AM Last Post By: Rudy Velthuis (...
Alan Jordan

Posts: 228
Registered: 3/2/01
RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 3, 2016 2:38 PM
I am using an API from AutoDesk and need a collection of their lines which are defined as SketchLines. VB for this looks like this Dim oLines(1 to 4) as Sketchlines. and then Set oLines(1) = oKSketch.SketchLines.Add.... where: Sketchlines is a previously defined object.

What is the syntax in Delphi for handling a collection of items like this? Here is what I have tried without success:
FLines : TList;
FLines:=TList.Create;

then in assigning the line to the TList:
function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look at the count it has indexed
end;

This compiles fine, but not sure how to access the items as SketchLine. What am I missing?
Andrew Baylis

Posts: 27
Registered: 7/19/00
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 3, 2016 4:58 PM   in response to: Alan Jordan in response to: Alan Jordan
On 4/07/2016 7:38 AM, Alan Jordan wrote:
I am using an API from AutoDesk and need a collection of their lines which are defined as SketchLines. VB for this looks like this Dim oLines(1 to 4) as Sketchlines. and then Set oLines(1) = oKSketch.SketchLines.Add.... where: Sketchlines is a previously defined object.

What is the syntax in Delphi for handling a collection of items like this? Here is what I have tried without success:
FLines : TList;
FLines:=TList.Create;

then in assigning the line to the TList:
function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look at the count it has indexed
end;

This compiles fine, but not sure how to access the items as SketchLine. What am I missing?
Assuming the Result doesn't get destroyed anywhere (you are storing a
pointer to the result), you can access the object via a direct cast:

ThisLine = SketchLine(Flines[index]);

Cheers,
Andrew
Alan Jordan

Posts: 228
Registered: 3/2/01
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 4, 2016 7:23 AM   in response to: Andrew Baylis in response to: Andrew Baylis
Andrew Baylis wrote:
On 4/07/2016 7:38 AM, Alan Jordan wrote:
I am using an API from AutoDesk and need a collection of their lines which are defined as SketchLines. VB for this looks like this Dim oLines(1 to 4) as Sketchlines. and then Set oLines(1) = oKSketch.SketchLines.Add.... where: Sketchlines is a previously defined object.

What is the syntax in Delphi for handling a collection of items like this? Here is what I have tried without success:
FLines : TList;
FLines:=TList.Create;

then in assigning the line to the TList:
function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look at the count it has indexed
end;

This compiles fine, but not sure how to access the items as SketchLine. What am I missing?
Assuming the Result doesn't get destroyed anywhere (you are storing a
pointer to the result), you can access the object via a direct cast:

ThisLine = SketchLine(Flines[index]);

Cheers,
Andrew

Thanks, that compiles fine, but when I go to access the TList.Items I am getting a stack overflow. Here is the code to enter the first line which works:

function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
var
Ct : Integer;
begin
Ct:=FLines.Count; // Ct is zero here
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2));
FLines.Add(@Result);
Ct:=FLines.Count; // Ct is 1 here
end;

QUESTIONS: Does the Pointer to Result go to nil when the method exits?

Then when I want to access the line I am using this:

function TInventor.GetLine(Index : Integer) : SketchLine;
begin
Result:=SketchLine(Lines[Index]); // When I debug, this statement is in a continuous loop and never exits the function and gives the stack overflow.
end;

I don't understand how to access this, or what component I can use to store the lines. Any help is greatly appreciated.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 4, 2016 8:30 AM   in response to: Alan Jordan in response to: Alan Jordan
Alan Jordan wrote:

Andrew Baylis wrote:
On 4/07/2016 7:38 AM, Alan Jordan wrote:
I am using an API from AutoDesk and need a collection of their
lines which are defined as SketchLines. VB for this looks like
this Dim oLines(1 to 4) as Sketchlines. and then Set oLines(1) =
oKSketch.SketchLines.Add.... where: Sketchlines is a previously
defined object.

What is the syntax in Delphi for handling a collection of items
like this? Here is what I have tried without success: FLines :
TList; FLines:=TList.Create;

then in assigning the line to the TList:
function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2
: T2DCartVector) : SketchLine; begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1),
CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look
at the count it has indexed end;

This compiles fine, but not sure how to access the items as
SketchLine. What am I missing?
Assuming the Result doesn't get destroyed anywhere (you are storing
a pointer to the result), you can access the object via a direct
cast:

ThisLine = SketchLine(Flines[index]);

Cheers,
Andrew

Thanks, that compiles fine, but when I go to access the TList.Items I
am getting a stack overflow. Here is the code to enter the first
line which works:

function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 :
T2DCartVector) : SketchLine; var
Ct : Integer;
begin
Ct:=FLines.Count; // Ct is zero here
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1),
CreateCoord(Pt2)); FLines.Add(@Result);
Ct:=FLines.Count; // Ct is 1 here
end;

QUESTIONS: Does the Pointer to Result go to nil when the method exits?

Then when I want to access the line I am using this:

function TInventor.GetLine(Index : Integer) : SketchLine;
begin
Result:=SketchLine(Lines[Index]); // When I debug, this statement
is in a continuous loop and never exits the function and gives the
stack overflow. end;

I don't understand how to access this, or what component I can use to
store the lines. Any help is greatly appreciated.

We cannot answer your question. From your code snippet we have no idea
what is Sketchline. If it's really an object you should use
Flines.Add(result) as you want to add the object and not the address of
the result!
Andrew Baylis

Posts: 27
Registered: 7/19/00
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 4, 2016 10:35 PM   in response to: Alan Jordan in response to: Alan Jordan
On 5/07/2016 12:23 AM, Alan Jordan wrote:
Andrew Baylis wrote:
On 4/07/2016 7:38 AM, Alan Jordan wrote:
I am using an API from AutoDesk and need a collection of their lines which are defined as SketchLines. VB for this looks like this Dim oLines(1 to 4) as Sketchlines. and then Set oLines(1) = oKSketch.SketchLines.Add.... where: Sketchlines is a previously defined object.

What is the syntax in Delphi for handling a collection of items like this? Here is what I have tried without success:
FLines : TList;
FLines:=TList.Create;

then in assigning the line to the TList:
function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look at the count it has indexed
end;

This compiles fine, but not sure how to access the items as SketchLine. What am I missing?
Assuming the Result doesn't get destroyed anywhere (you are storing a
pointer to the result), you can access the object via a direct cast:

ThisLine = SketchLine(Flines[index]);

Cheers,
Andrew

Thanks, that compiles fine, but when I go to access the TList.Items I am getting a stack overflow. Here is the code to enter the first line which works:

function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 : T2DCartVector) : SketchLine;
var
Ct : Integer;
begin
Ct:=FLines.Count; // Ct is zero here
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1), CreateCoord(Pt2));
FLines.Add(@Result);
Ct:=FLines.Count; // Ct is 1 here
end;

QUESTIONS: Does the Pointer to Result go to nil when the method exits?

Then when I want to access the line I am using this:

function TInventor.GetLine(Index : Integer) : SketchLine;
begin
Result:=SketchLine(Lines[Index]); // When I debug, this statement is in a continuous loop and never exits the function and gives the stack overflow.
end;

I don't understand how to access this, or what component I can use to store the lines. Any help is greatly appreciated.
Alan, just guessing as you haven't shown us enough code.... is Lines a
property of your object that calls GetLine? If so, that would explain
the infinite loop and stack overflow as you access the property within
its accessor.

Andrew
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 4, 2016 12:12 PM   in response to: Alan Jordan in response to: Alan Jordan
Alan wrote:

I am using an API from AutoDesk and need a collection of their lines
which are defined as SketchLines. VB for this looks like this Dim
oLines(1 to 4) as Sketchlines. and then Set oLines(1) =
oKSketch.SketchLines.Add.... where: Sketchlines is a previously
defined object.

Objects in VB are implemented as interfaced COM objects.

What is the syntax in Delphi for handling a collection of items like
this?

In Delphi, to hold a list of interfaced objects, you can use TInterfaceList
or the Generic TList<T>. Do not use the non-Generic TList, as it does not
manage interface reference counts.

FLines: TInterfaceList;
...
FLines := TInterfaceList.Create;

FLines: TList<SketchLine>;
...
FLines := TList<SketchLine>.Create;


FLines.Add(@Result); // this doesn't complain and when I look at the
count it has indexed

The Result is already a pointer, so don't use the @ operator.

FLines.Add(Result);


This compiles fine, but not sure how to access the items as
SketchLine. What am I missing?

You are missing a typecast. If using TList:

FLines: TList;
line: SketchLine;
...
line := SketchLine(FLines[index]);


If using TInterfaceList:

FLines: TInterfaceList;
line: SketchLine;
...
line := FLines[index] as SketchLine;


If using TList<T>, no type-cast is needed:

FLines: TList<SketchLine>;
line: SketchLine;
...
line := FLines[index];

--
Remy Lebeau (TeamB)

Alan Jordan

Posts: 228
Registered: 3/2/01
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 4, 2016 1:36 PM   in response to: Alan Jordan in response to: Alan Jordan
As always thanks Remy.
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: RAD 2010 need a collection of Objects?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 6, 2016 4:53 AM   in response to: Alan Jordan in response to: Alan Jordan
Alan Jordan wrote:

function TInventor.AddLineSingle(Sketch : PlanarSketch; Pt1, Pt2 :
T2DCartVector) : SketchLine;
begin
Result:=Sketch.SketchLines.AddByTwoPoints(CreateCoord(Pt1),
CreateCoord(Pt2)); // this draws the line ok
FLines.Add(@Result); // this doesn't complain and when I look at
the count it has indexed
end;

This compiles fine, but not sure how to access the items as
SketchLine. What am I missing?

This very much depends on the type of Result. Assuming it is an object
and not a record, then there is no need to use the @ operator (which
returns the address). Actually, you should NOT use it.

Do this instead:

FLines.Add(Pointer(Result));

Result is a reference already, so you can cast to pointer. Do not take
the address of Result, as that is the address of the local variable and
that will be gone (and invalid) when the function ends.

To retrieve the sketchline at index I, you can do:

mySketchLine := TObject(FLines[I]) as SketchLine;

You should better use TObjectList, so you don't have to do a hard cast
to TObject:

FLines: TObjectList;
...
Flines.Add(Result);
...
MySketchLine := FLines[I] as SketchLine;

Now, if you have a version of Delphi that supports generics, you should
use its generic TObjectList<T>:

FLines: TObjectList<SketchLine>;
...
Flines := TObjectlist<SketchLines>.Create;
...
FLines.Add(Result); // no cast required
...
MySketchLine := FLines[I];

Every object stored in a TObjectList<SketchLine> is a SketchLine, and
the compiler knows this, so there is no need to cast anymore.

FWIW, TObjectList has a property OwnsObjects that is True by default.
If it is true, it will free all objects it contains as soon as you
Clear or Free the list. This is true for TObjectList as well as for
TObjectList<T>.

If you don't want your SketchLines to be freed by FLines, you should
set that property to False.

--
Rudy Velthuis http://www.rvelthuis.de

"In all large corporations, there is a pervasive fear that
someone, somewhere is having fun with a computer on company time.
Networks help alleviate that fear." -- John C. Dvorak.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02