传递从Excel VBA变量数组到WCF功能 [英] Passing a Variant Array from Excel VBA to a WCF Function
问题描述
我拼命想做到这一点。
I'm trying desperately to do this.
我已经能够复制这个帖子上发现的行为。
I've been able to replicate the behavior found on this post.
的 http://damianblog.com/2009/07/05/excel- WCF /评论页-1 /#评论-64232
不过,我无法将数组传递给暴露WCF功能。
however, I am unable to pass an array to an exposed wcf function.
我的WCF服务的工作原理是这样的(我也尝试过使用整型数组)
My WCF Service works like this (I have also tried to use arrays of int)
public object[] GetSomeArray()
{
return new object[] { 1, 2, 3, 4};
}
public object[] ReturnSomeArray(object someArray)
{
object[] temp = (object[]) someArray;
for (int i = 0; i < temp.Length; i++)
{
temp[i] = (int)temp[i] + 1;
}
return temp;
}
我的VBA代码如下所示。
my VBA code looks like this.
Dim addr As String
...
Dim service1 As Object
Set service1 = GetObject(addr)
Dim columnsVar
columnsVar = Array(1, 2, 3)
Dim anotherArray As Variant
anotherArray = service1.ReturnSomeArray(columnsVar)
我一直都对上述的最后一行的问题。我不明白为什么,如果我能够从我的WCF服务返回一个数组,我不能通相同的数组作为参数传递给另一个WCF功能。结果
I always have problems on the last line above. I don't understand why if I'm able to return an array from my WCF service that I'm not able pass that same array as a parameter to another WCF function.
我得到一个序列化错误
任何帮助,将不胜感激。
Any help would be appreciated.
推荐答案
我有类似的问题类型不匹配
错误仅如果我声明数组变量在VBA中是这样的:
I have similar problems with Type mismatch
error only if I declare array variable in VBA in this way:
Dim anotherArray() As Variant
,但如果该变量被以这种方式定义的误差消失:
but the error disappears if the variable is defined in this way:
Dim anotherArray As Variant
您和我类似的解决方案之间的一些其他方面的差异是:
Some other differences between your and my similar solutions are:
//C#- my solution- without array[] definition:
public object[] ReturnSomeArray(object someArray)
//VBA- my solution -without array() definition:
Dim someArray As Variant
修改:2013年8月28日
是 C#-Excel-互操作工作我更喜欢尝试和放大器;搜索解决方案的测试方法。如果有什么工作,那我坚持下去,有时我错过指示溶液或逻辑的来源。
Working with C#-Excel-Interop I prefer try&test way of searching solution. If anything works then I stick to it and sometime I miss to indicate the source of the solution or logic.
下面,你会发现代码,其中包括 LINQ
来使用数组操作。这些代码片段在这两个工程方向 - 获得C#的数据VBA >>传回C#进行排序>>返回VBA。我希望它会帮助你更多的最终解决您的问题。
Below you will find code which includes LINQ
to operate with arrays. These code snippets works in both direction- get data from C# to VBA >> pass it back to C# for sorting >> return to VBA. I hope it will help you more to finally solve your problems.
第一:一些C#代码
public object[] GetSomeArray()
{
return new object[] { 5, 2, 1, 7, 9, 1, 5, 7 };
}
public double[] ArraySorted(object tablica)
{
object[] obj = (object[])tablica;
var filtr = from i in obj
orderby Convert.ToDouble(i)
select Convert.ToDouble(i);
double[] wynik = (double[])filtr.ToArray();
return wynik;
}
二:一些VBA代码
Sub qTest_second_attempt()
'declare array variable
Dim tmp()
'and other variables
Dim addr As String
addr = "UDFArrayLinqTest.ArrayLinq"
'get references
Dim service1 As Object
Set service1 = CreateObject(addr)
'get array from C#
tmp = service1.GetSomeArray()
'pass this array to C# back to sort it
Dim someArray As Variant
someArray = service1.ArraySorted(tmp)
'check the result in Immediate window
Debug.Print Join(WorksheetFunction.Transpose(WorksheetFunction.Transpose(someArray)))
'result: 1 1 2 5 5 7 7 9
End Sub
这篇关于传递从Excel VBA变量数组到WCF功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!