String Array Object to an ArrayList

  • Thread starter Thread starter Jeff Johnson
  • Start date Start date
J

Jeff Johnson

Hi,

I've begun converting an ASP site over to .NET and I'm a novice at both the
new platform as well as C#.

I have a COM+ object that returns a string array when it is called. The
size of the array can vary depending on the parameters passed.

What I need to do is loop through the returned array and if applicable write
the array element to the screen.

I'd like to know if an ArrayList is the best way to go or is there something
else in C# I should use. I'm not adding, deleting or appending anything to
the returned array.

Secondly, what would be the syntax for this?

What I have so far is the declaration:
ComLogicEngine.ComPlusLogic oConfig = new
ComLogicEngine.ComPlusLogic();

And the call to the COM+ object:
oConfig.Execute("blah", "blah", "blah");


Thank you for your help,

JJ
 
Jeff Johnson said:
I've begun converting an ASP site over to .NET and I'm a novice at both the
new platform as well as C#.

I have a COM+ object that returns a string array when it is called. The
size of the array can vary depending on the parameters passed.

What I need to do is loop through the returned array and if applicable write
the array element to the screen.

I'd like to know if an ArrayList is the best way to go or is there something
else in C# I should use. I'm not adding, deleting or appending anything to
the returned array.

Then why do you need to do anything with it? Why not just iterate
through it, eg with:

foreach (string x in myStringArray)
{
....
}
 
The Array part syntax:

string[] myArray;

myArray = new String[] { "test1", "Test2", "TEST3" };

foreach (string s in myArray) {

Console.WriteLine(s);

}
 
I tried using the syntax below, substituting my call to the COM+ object
where you entered "test1"... and I received an error. I have been
successful in binding it to a DataGrid. I'm not sure if that helps in
describing the state of object when it is returned.

Do you have any other ideas on how I could approach this?

JJ

Mythran said:
The Array part syntax:

string[] myArray;

myArray = new String[] { "test1", "Test2", "TEST3" };

foreach (string s in myArray) {

Console.WriteLine(s);

}





Jeff Johnson said:
Hi,

I've begun converting an ASP site over to .NET and I'm a novice at both the
new platform as well as C#.

I have a COM+ object that returns a string array when it is called. The
size of the array can vary depending on the parameters passed.

What I need to do is loop through the returned array and if applicable write
the array element to the screen.

I'd like to know if an ArrayList is the best way to go or is there something
else in C# I should use. I'm not adding, deleting or appending anything to
the returned array.

Secondly, what would be the syntax for this?

What I have so far is the declaration:
ComLogicEngine.ComPlusLogic oConfig = new
ComLogicEngine.ComPlusLogic();

And the call to the COM+ object:
oConfig.Execute("blah", "blah", "blah");


Thank you for your help,

JJ
 
Hrm....

string[] myArray = <call to com to receive array>;

foreach yadda yadda yadda...

Mythran



Jeff Johnson said:
I tried using the syntax below, substituting my call to the COM+ object
where you entered "test1"... and I received an error. I have been
successful in binding it to a DataGrid. I'm not sure if that helps in
describing the state of object when it is returned.

Do you have any other ideas on how I could approach this?

JJ

Mythran said:
The Array part syntax:

string[] myArray;

myArray = new String[] { "test1", "Test2", "TEST3" };

foreach (string s in myArray) {

Console.WriteLine(s);

}





Jeff Johnson said:
Hi,

I've begun converting an ASP site over to .NET and I'm a novice at both the
new platform as well as C#.

I have a COM+ object that returns a string array when it is called. The
size of the array can vary depending on the parameters passed.

What I need to do is loop through the returned array and if applicable write
the array element to the screen.

I'd like to know if an ArrayList is the best way to go or is there something
else in C# I should use. I'm not adding, deleting or appending anything to
the returned array.

Secondly, what would be the syntax for this?

What I have so far is the declaration:
ComLogicEngine.ComPlusLogic oConfig = new
ComLogicEngine.ComPlusLogic();

And the call to the COM+ object:
oConfig.Execute("blah", "blah", "blah");


Thank you for your help,

JJ
 
No dice.

I get an "Cannot implicity convert type 'object' to 'string[]'" error when I
try that.

Mythran said:
Hrm....

string[] myArray = <call to com to receive array>;

foreach yadda yadda yadda...

Mythran



Jeff Johnson said:
I tried using the syntax below, substituting my call to the COM+ object
where you entered "test1"... and I received an error. I have been
successful in binding it to a DataGrid. I'm not sure if that helps in
describing the state of object when it is returned.

Do you have any other ideas on how I could approach this?

JJ

Mythran said:
The Array part syntax:

string[] myArray;

myArray = new String[] { "test1", "Test2", "TEST3" };

foreach (string s in myArray) {

Console.WriteLine(s);

}





Hi,

I've begun converting an ASP site over to .NET and I'm a novice at
both
the
new platform as well as C#.

I have a COM+ object that returns a string array when it is called. The
size of the array can vary depending on the parameters passed.

What I need to do is loop through the returned array and if
applicable
write
the array element to the screen.

I'd like to know if an ArrayList is the best way to go or is there something
else in C# I should use. I'm not adding, deleting or appending
anything
to
the returned array.

Secondly, what would be the syntax for this?

What I have so far is the declaration:
ComLogicEngine.ComPlusLogic oConfig = new
ComLogicEngine.ComPlusLogic();

And the call to the COM+ object:
oConfig.Execute("blah", "blah", "blah");


Thank you for your help,

JJ
 
Jeff Johnson said:
No dice.

I get an "Cannot implicity convert type 'object' to 'string[]'" error when I
try that.

So cast it to a string[] - assuming you're absolutely sure that it
*does* return a string array.
 
I believe that it does return a string array.

Maybe to help explain things, I'm trying to convert this code from
ASP/VBScript to C#:

arrData = comPlusObject.Execute("a", "b", "c")

for(i=0 to ubound(arrData))
response.write(arrData(i))
next

In ASP, that call returns an array that is easily parsed. I'm not sure why
what has been suggested doesn't work. I've found that I can bind what's
returned from the call to the COM+ object to a datagrid without any issues.

JJ


Jon Skeet said:
Jeff Johnson said:
No dice.

I get an "Cannot implicity convert type 'object' to 'string[]'" error when I
try that.

So cast it to a string[] - assuming you're absolutely sure that it
*does* return a string array.
 
I've been in a similar situation. I solved the problem by implementing a
method that first casts the returned array and then converts the
returned array to a String array.

Using the method o2s (you might find a better name) below you can do
something like:

string[] arrData = o2s(comPlusObject.Execute("a", "b", "c"));
// Then just use the String array
Console.WriteLine( arrData[0] );

The method has been designed to access Lotus Notes String item values
using the Domino COM interface (domlib.tlb).

The number of String elements returned will be equal to the number of
elements returned by the called COM method.

This could probably also have been done by a simple System.Array.CopyTo,
but in order to avoid null pointer exceptions I wanted to set the first
String element to an empty string if the COM method returns null.

I'm using the code below in a console application. Depending on your
implementation you may have to remove the static attribute.

static string[] o2s ( Object obj){
// cast the System.Object to a System.Array
Array objArray = (Array) obj;

// create a string array of same size
string[] strArray = new string[objArray.Length];

// loop through the System.Array and populate
// the String Array
for (int i = 0; i < objArray.Length; i++) {
if (objArray.GetValue(i) == null)
// In order to avoid null pointer exceptions
// we will return an empty string in the first
// element if the first element retruned by
// the call to the COM method is null.
strArray = "";
else
strArray = (string)objArray.GetValue(i).ToString();
}
return strArray;
}


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top