如何处理从VBA中的C#方法返回的字符串数组 [英] How do I handle a string array returned from a C# Method in VBA
问题描述
我已经用C#编写的组件,它返回一个字符串数组,在C#代码如下:
I have written an assembly in C# which returns a string array, the C# code is below:
[ComVisible(true)]
public class PostcodeFinder
{
public string[] SearchPostcodes(string postCode)
{
var searchService = new QuickAddress("http://x.x.x.x:xxxx/")
{Engine = QuickAddress.EngineTypes.Singleline, Flatten = true};
var mPicklist = searchService.Search("GBR", postCode, PromptSet.Types.OneLine);
var x = mPicklist.Picklist.Items.Count();
var resultsToReturn = new string[x];
for (var i = 0; i < x; i++)
{
resultsToReturn[i] = mPicklist.Picklist.Items[i].PartialAddress;
}
return resultsToReturn;
}
}
我已经然后建立了这个集会,是一定要勾选在注册为COM Interop
在属性框中。然后在Microsoft Access,我在 .TLB
文件添加到引用和创建的窗体与一对夫妇的控件(其中一个是列表框
控制名为 lstResults
)。在主按钮控制,我有以下VBA代码:
I have then built this assembly, being sure to tick the Register for COM interop
box in properties. Then in Microsoft Access, I have added the .tlb
file to the references and created a form with a couple of controls (one of which is a Listbox
control named lstResults
). Under the main button control, I have the following VBA code:
Private Sub btnSearch_Click()
Dim postcodeToSearch As String
postcodeToSearch = Me.txtPostcode
Dim c As New PostcodeFinder
Dim results
results = c.SearchPostcodes(postcodeToSearch)
End Sub
编辑:但是当我查询即时此运行没有错误,与窗口结果
把下面的一些虚拟代码,让我把一个断点,我得到以下错误后:
This runs without error, however when I query the Immediate window with ?results
after putting some dummy code below to allow me to place a breakpoint, I get the following error:
运行时错误'13' - 类型不匹配
Run-time error '13' - Type mismatch
切实我想重写下面的C#在VBA代码:
Effectively I want to rewrite the following C# code in VBA:
var results = c.SearchPostcodes(postcodeToSearch);
foreach(var x in results)
{
lstResults.Items.Add(x);
}
在此先感谢
Thanks in advance
推荐答案
更新答案:它不会返回的的String []
,尝试返回对象
而不是:
Updated Answer: Instead of returning string[]
, try returning object
instead:
[ComVisible(true)]
public class PostcodeFinder
{
public object SearchPostcodes(string postCode)
{
//Unchanged code
return (object)resultsToReturn;
}
}
您仍然会得到即时的类型不匹配错误窗口表演时?结果
(您需要指定一个索引,如?结果(0)
),但您可能够通过数组迭代:
You will still get the type mismatch error in the immediate window when performing ?results
(you need to specify an index such as ?results(0)
), however you are able to iterate through the array as:
results = c.SearchPostcodes(postcodeToSearch)
Dim result As Variant
For Each result In results
MsgBox result
Next result
原来的答案:您需要实例化PostcodeFinder类。在你btnSearch_Click子程序,请尝试:
Original Answer: You need to instantiate the PostcodeFinder class. In your btnSearch_Click subroutine, try:
Dim c As New PostcodeFinder
Dim results
results = c.SearchPostcodes(postcodeToSearch)
另外,你可以声明从实例作为单独的:
Alternatively, you could separate the declaration from the instantiation as:
Dim c As PostcodeFinder
Dim results
Set c = New PostcodeFinder
results = c.SearchPostcodes(postcodeToSearch)
这篇关于如何处理从VBA中的C#方法返回的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!