Method return List of type
Good afternoon I am making a method of a class that I want to return a list of objects of another class that I have defined, example:
ClassMethod myMethod() As list of MyPackage.MyClass
{
}
But it gives me a syntax error, I try this and it works:
ClassMethod myMethod() As %ListOfObjects
{
}
But I would like to keep the typing.
Is there a way?
Comments
Keep the second version
and build that List of objects with - As list of MyPackage.MyClass
list.insert(obj1)
list.insert(obj2)
and so on..
Please feel free to send me sample code and I can make it for you too.
Thanks Neerav I will do it like this
I hope it worked for you.
I am also available as a consultant for any short/long term projects . Pls let me know
https://community.intersystems.com/post/certified-intersystems-professional-available-contract
ObjectScript variables are untyped, so preserving the type isn't necessary.
You'll build the list by adding your MyPackage.MyClass objects to it, then you'll return the %ListOfObjects, then you'll use that list's methods to manipulate those objects. For instance, set mything = mylist.GetAt(1) will give you an object that is identical to the MyPackage.MyClass object you put in the list with all of its properties an methods.
The right way to describe a list of a particular type of object is:
ClassMethod myMethod() As %ListOfObjects(ELEMENTTYPE="MyPackage.MyClass")
This is most relevant from the perspective of XML projections - e.g., web services. You can also define a subclass of %ListOfObjects with the ELEMENTTYPE parameter overridden for reuse. See https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=G…