Looping through IObjectList - Error
CapableObjects Forums
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Looping through IObjectList - Error Expand / Collapse
Author
Message
Posted 2009-12-03 17:09:18
Forum Guru

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru

Group: Forum Members
Last Login: 2010-05-16 05:14:12
Posts: 63, Visits: 355
I am getting an error while trying to loop through an IObjectList

********************

oResultList := myps.Evaluate( 'Person.allinstances') as IObjectList;

for each oObj:IObject in oResultList do

begin

//other code

oPerson :=oObj.AsIObject as person;//error on this line

//other code

end;

*******************************

What does this error mean ?

 

 

***************************************

System.InvalidCastException was unhandled by user code
  Message="Unable to cast object of type 'Eco.Framework.Impl.Frontside.DefaultFrontsideLocator' to type 'Package_1.Person'."
  Source="Eco54Nuts"
  StackTrace:
       at Eco54Nuts.FormMain.btnSendMail_Click(Object sender, EventArgs e) in E:\54Nuts\Copy (2) of Copy of Eco54Nuts Rank subgroup\Eco54Nuts\MainForm.pas:line 156
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException:

Post #4604
Posted 2009-12-04 04:07:45
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410
Here is how I use IObjectList for collections in C#:

IObjectList oclResult = ecoSpace.GetEcoService<IOclPsService>().Execute(null, null, "Person.AllInstances", -1, 0);
Person persons = oclResult.GetAsIList<Person>();
foreach ( var person in Persons ) {
    // access person for your logic
}

I'll take a stab at what the Delphi syntax would be, but I'll probably be off a little.

oResultList := myps.Evaluate( 'Person.allinstances') as IObjectList;
persons := oResultList.GetAsIList<Person>();
for each person: Person in oResultList do begin
    // access person for your logic
end;

I'm not sure how Delphi uses generic methods, so the syntax is probably wrong.

Also, if your result is not a collection you can use IObjectList.AsObject to return an instance of the object returned from the ocl expression.  You will need to cast it to the proper type.  This works for value types as well (i.e. int/integer, bool/booloean).

Brian

Post #4605
Posted 2009-12-04 17:47:37
Forum Guru

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru

Group: Forum Members
Last Login: 2010-08-12 17:32:24
Posts: 51, Visits: 600
Hi Venks

oPerson :=oObj.AsIObject as person;//error on this line

Should be oObj.AsObject not oObj.AsIObject

Nick

Post #4606
Posted 2009-12-04 23:38:34
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410
Just noticed a bug in the code.

Person persons = oclResult.GetAsIList<Person>();

should be:

IList<Person> persons = oclResult.GetAsIList<Person>();

or

var persons = oclResult.GetAsIList<Person>();

Brian

Post #4609
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: HansKarlsen, Jonas Hogstrom, PeterMorris

Permissions Expand / Collapse

All times are GMT +1:00, Time now is 9:51

Powered By InstantForum.NET v4.1.4 © 2012
Execution: 0,328. 9 queries. Compression Disabled.