Using Object

T

Tom Woods

I'm using a library that returns an Object. The result can be either an
array of strings or an XML document.

First step, how can I cast the result (Object) to get access to the array
elements?

MyResults = MyCalcs.GetDetails( MySettings, MyInformation );

How can I get MyResults[51] to get the specific data out of the array?

TIA,

Tom
 
T

Tom Porterfield

Tom said:
I'm using a library that returns an Object. The result can be either an
array of strings or an XML document.

First step, how can I cast the result (Object) to get access to the array
elements?

MyResults = MyCalcs.GetDetails( MySettings, MyInformation );

How can I get MyResults[51] to get the specific data out of the array?

Use the "as" keyword to convert the object to a string array. Test to
make sure the conversion works and then access the elements in the array
as you normally would. Based on your example above:

string [] MyResults;

object result = MyCalcs.GetDetails(MySettings, MyInformation);
MyResults = result as string[];

if (MyResults != null)
{
// it's a string array
// access elements as you would any other array
}
else
{
// it's not a string array
// you could convert and test for XmlDocument in a similar way
}
 
T

Tom Woods

I believe what you sent would work if the array was just strings. However,
I found out from the vendor that the return "object" can have a combination
of strings, numbers, and arrays. For example, it may look like this.


MyResults(0) ="Bond #1"

MyResults(1) = 6.25

MyResults(2) = "01/01/2010"

MyResults(3)(0,0) = 1000000 'For returning cashflows

MyResults(3)(0,1) = 100

MyResults(3)(0,2) = 900



What can be done to read this data?

Thanks,

Tom




Tom Porterfield said:
Tom said:
I'm using a library that returns an Object. The result can be either an
array of strings or an XML document.

First step, how can I cast the result (Object) to get access to the array
elements?

MyResults = MyCalcs.GetDetails( MySettings, MyInformation );

How can I get MyResults[51] to get the specific data out of the array?

Use the "as" keyword to convert the object to a string array. Test to
make sure the conversion works and then access the elements in the array
as you normally would. Based on your example above:

string [] MyResults;

object result = MyCalcs.GetDetails(MySettings, MyInformation);
MyResults = result as string[];

if (MyResults != null)
{
// it's a string array
// access elements as you would any other array
}
else
{
// it's not a string array
// you could convert and test for XmlDocument in a similar way
}
 
T

Tom Porterfield

Tom said:
I believe what you sent would work if the array was just strings. However,
I found out from the vendor that the return "object" can have a combination
of strings, numbers, and arrays. For example, it may look like this.


MyResults(0) ="Bond #1"

MyResults(1) = 6.25

MyResults(2) = "01/01/2010"

MyResults(3)(0,0) = 1000000 'For returning cashflows

MyResults(3)(0,1) = 100

MyResults(3)(0,2) = 900

What can be done to read this data?

I would expect the vendor to map this for you. Surely the data elements
are not random but instead in some logical order.
 
T

Tom Woods

Thanks Tom for the input. Luckily, the vendor allows the result (object) to
be return as an XML Document. Now, it's time to figure out how to access
the XmlDocument.

Thanks,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top