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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PowerBuilder .NET Worknig with an array of objects.

Status
Not open for further replies.

dbgetty

Programmer
Aug 14, 2010
2
US
Good Morning,
I'm trying to understand how to accept a list of objects returned from a .NET assembly (using COM interop).
My .NET class is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
public class Person //this is our complex type
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}

private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}

public class PeopleManager
{
public Person GetPerson()
{
Person p = new Person(); //simple way to intantiate a Person
p.Age = 33;
p.Name = "Joe Smith";
return p;
}

public Person[] GetPeople() //returns an array of Person
{
List<Person> personList = new List<Person>();
personList.Add(new Person() { Age = 33, Name = "Joe Smith" });
personList.Add(new Person() { Age = 42, Name = "Sally Sue" });

return personList.ToArray();
}
}


}

My PowerBuilder class:

OLEOBJECT peopleManager, person
integer li_RC

peopleManager = CREATE OLEObject
person = CREATE OLEObject

li_RC = peopleManager.ConnectToNewObject("ClassLibrary1.PeopleManager")

IF li_RC <> 0 THEN
MessageBox("Error", "Error #" + string(li_RC) + " - Component Installation Error")
ELSE

//this section works perfectly
person = peopleManager.GetPerson()
string ls_Name
integer li_Age
ls_Name = person.Name
li_Age = person.age
DESTROY person
MessageBox("Person", "Name: " + ls_Name + " Age: " + string(li_Age))

//this section is my problem!
OLEObject lole_PersonList[]
lole_PersonList = peopleManager.GetPeople().ToArray()
integer li_Idx
li_RC = UpperBound(lole_PersonList)
FOR li_Idx = 1 TO li_RC
person = CREATE OLEObject

//this assignment doesn't work, person is an invalid reference
//so the next line crashes
person = lole_PersonList[li_Idx]

ls_Name = person.name
li_Age = person.age
DESTROY person
MessageBox("Person", "Name: " + ls_Name + " Age: " + string(li_Age))
NEXT

END IF

DESTROY peopleManager
RETURN li_RC

Note that GetPerson call in the first section works. But trying to use the array of objects fails. I can tell from the debugger that I have an array of two "person" objects after the call to getPeople but I can't seem to access them. I get a NULL Object Reference error. Even if I try to access without the casting (i.e. ls_Name = lole_PersonList[li_Idx].Name) I get the same error. Clearly I am a beginner with using COM. Any help would be greatly appreciated.

Thanks,
Dave
 
What if you gave your class a method to return each 'person' from the array based on an index argument? Then you could loop through the array. Just an idea.

Matt

"Nature forges everything on the anvil of time"
 
Thank you for the suggestion Matt. That may turn out to be the wisest course of action.

- Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top