有关Com+ 反回数组问题

MyGhosts 2005-06-29 11:16:20
请教:
我从Com+ 返回一个二维数组,代码如下:

.......
object obj1 = new object();
int success = class.method( ...., ref obj1);
.......

obj1 就是从COM+ 返回的二维数组; 请问如何循环取得里面的值?
我尝试过两种转换,

ArrayList al = (ArrayList) obj1; // 这个会提示错误,不能转换;
string[,] strArray = (string[,]) obj1; // 这个也会提示错误;
object[,] obj = (object[,]) obj1; // 这个可以转换,但不懂如何循环此二维数组,

望大家指导下,thank you.



...全文
97 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mba9001 2005-06-30
  • 打赏
  • 举报
回复
Com+?
MyGhosts 2005-06-30
  • 打赏
  • 举报
回复
b.Length 完全相当于 b.GetLength(0)

---------------------------
好像相当于 b.GetLength(1);


--------------------------------------------------------

不好意思,俺滴错了;
crc2006 2005-06-30
  • 打赏
  • 举报
回复
运行一下代码就知道我说的没错了。
俞庆平 2005-06-30
  • 打赏
  • 举报
回复
MSDN中关于Array类型的解释是:

GetLength(int dimension)
Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

[C#]
public int GetLength(
int dimension
);
dimension
A zero-based dimension of the Array whose length needs to be determined.

A 32-bit integer that represents the number of elements in the specified dimension.

//Length
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
MyGhosts 2005-06-30
  • 打赏
  • 举报
回复
b.Length 完全相当于 b.GetLength(0)

---------------------------
好像相当于 b.GetLength(1);
crc2006 2005-06-30
  • 打赏
  • 举报
回复
回楼上:

不是这样的。

b.Length 完全相当于 b.GetLength(0)
crc2006 2005-06-30
  • 打赏
  • 举报
回复
@ begincsdn(CNetware)

Length 的含义在规则的多维数组里面的确是所有元素的定义。
但我那个数组是个锯齿型的。
而我现在的理解是,锯齿型数组其实是“数组的数组”,区别在于,他的每一维的定义都是一个独立的 System.Array 的实现, 所以我那样用是没错的。
俞庆平 2005-06-29
  • 打赏
  • 举报
回复
for (int i = 0; i < b.Length; i++) {
for (int j = 0; j < b[i].Length; j++) {
Console.WriteLine("b[{0}][{1}] = {2}", i, j, b[i][j]);
}
}
这里是有问题的,因为b.Length取得是所有维度元素的总数。
显然这会将同一元素不知道访问了多少遍。
crc2006 2005-06-29
  • 打赏
  • 举报
回复
using System;

public class Test {
public static void Main() {
Bar();

Console.WriteLine("\n\n");

Foo();
}

// 锯齿型数组
public static void Foo() {
string[][] b = new string[2][];

b[0] = new string[] {"hello", "welcome"};
b[1] = new string[] {"ok", "haha", "wokao", "shit"};

for (int i = 0; i < b.Length; i++) {
for (int j = 0; j < b[i].Length; j++) {
Console.WriteLine("b[{0}][{1}] = {2}", i, j, b[i][j]);
}
}
}

// 二维数组
public static void Bar() {
string[,] a = new string[3, 2] {{"0_0", "0_1"}, {"1_0", "1_1"}, {"2_0", "2_1"}};

for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++) {
Console.WriteLine("a[{0}, {1}] = {2}", i, j, a[i, j]);
}
}
}
}
crc2006 2005-06-29
  • 打赏
  • 举报
回复
using System;

public class Test {
public static void Main() {
Bar();

Console.WriteLine("\n\n");

Foo();
}

// ¾â³ÝÐÍÊý×é
public static void Foo() {
string[][] b = new string[2][];

b[0] = new string[] {"hello", "welcome"};
b[1] = new string[] {"ok", "haha", "wokao", "shit"};

for (int i = 0; i < b.Length; i++) {
for (int j = 0; j < b[i].Length; j++) {
Console.WriteLine("b[{0}][{1}] = {2}", i, j, b[i][j]);
}
}
}

// ¶þάÊý×é
public static void Bar() {
string[,] a = new string[3, 2] {{"0_0", "0_1"}, {"1_0", "1_1"}, {"2_0", "2_1"}};

for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++) {
Console.WriteLine("a[{0}, {1}] = {2}", i, j, a[i, j]);
}
}
}
}
俞庆平 2005-06-29
  • 打赏
  • 举报
回复
不过,对于COM返回的对象,ToString()方法未必有效。
俞庆平 2005-06-29
  • 打赏
  • 举报
回复
二维数组或多维数组的引用方法的确如此。
恭喜。
MyGhosts 2005-06-29
  • 打赏
  • 举报
回复
object[,] detail = (object[,]) o2;
for(int n=0; n < detail.GetLength(0); n++)
{
for(int m=0; m < detail.GetLength(1); m++)
this.lbNote.Text += detail[n,m].ToString() + "<br>";
}

^_^ 搞定

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧