简体   繁体   English

用户定义类型和将数组传递给函数

[英]User-defined Type & passing Array to Function

I'm having troubles passing an Array from user-defined Type to a Function:我在将数组从用户定义的类型传递给函数时遇到了麻烦:

Defined Type (in a standard Module):定义类型(在标准模块中):

Public Type JPrinters
    Available() As String
    Default As String
End Type

Function to make a list of all available printers (std Module):列出所有可用打印机的功能(std 模块):

Function ListPrinters() As JPrinters

    Dim GetPrinters As Object: Set GetPrinters = CreateObject("WScript.Network").EnumPrinterConnections     
    Dim i As Integer
    Dim j() As String  'Array of Printer Names
    ReDim j(0 To GetPrinters.Count \ 2 - 1)

    For i = 0 To UBound(j)
        'Load this array element with the Name from the list
        j(i) = GetPrinters.Item(i * 2 + 1)
    Next i

    Dim k As JPrinters
    With k
        .Available = j
        .Default = Application.ActivePrinter
    End With

    ListPrinters = k
End Function

Sub to populate a ComboBox by an Array (which works fine, normally – std Module): Sub 用一个数组来填充一个 ComboBox(它工作正常,通常 - std 模块):

Sub PopulateComboBoxBy2DArray(Ctrl As Control, ByVal Source_1D_2D_Array As String, Optional DefaultValue As String)
    Ctrl.List = Source_1D_2D_Array
    If DefaultValue Then
        Ctrl.Value = DefaultValue
    End If
End Sub

Then I try to call everything on Workbook_Open:然后我尝试调用 Workbook_Open 上的所有内容:

Private Sub Workbook_Open()
    Dim Prt As JPrinters
    Prt = ListPrinters
    PopulateComboBoxBy2DArray List1.PrinterList, Prt.Available, Prt.Default
End Sub
  • All modules have Option Explicit defined.所有模块都定义了Option Explicit

And it just gives my Compile error: Type mismatch .它只是给出了我的Compile 错误: Type mismatch I tried a lot but cannot figure out why it won't accept the Prt.Available Array.我尝试了很多,但无法弄清楚为什么它不接受Prt.Available数组。 Any ideas?有任何想法吗?

(I will also be glad for any other suggestions regarding the code.) (对于有关代码的任何其他建议,我也很高兴。)

Thank you,谢谢,

J J

Resolved (thanks to Dave from Mr. Excel forums).已解决(感谢 Excel 先生论坛的 Dave)。 It goes like this:它是这样的:

Type:类型:

Type J_Type_Printers
    Available() As Variant
    Default As Variant
End Type

Function:功能:

Function J_Fx_ListPrinters() As J_Type_Printers
    Dim GetPrinters As Object
    Dim i As Integer
    Dim j() As Variant 'Array of Printer Names
    Dim k As J_Type_Printers

    Set GetPrinters = CreateObject("WScript.Network").EnumPrinterConnections
    ReDim j(0 To GetPrinters.Count \ 2 - 1)

    For i = 0 To UBound(j)
        'Load this array element with the Name from the list
        j(i) = GetPrinters.Item(i * 2 + 1)
    Next i

    With k
        .Available = j
        .Default = Application.ActivePrinter
    End With
    J_Fx_ListPrinters = k
End Function

Sub to populate combobox by array contents: Sub 按数组内容填充组合框:

Sub J_Sub_Controls_ComboBox_ListArray(ByVal Ctrl As ComboBox, ByVal SrcArray As Variant, Optional DefaultValue As Variant)
    Ctrl.List = SrcArray
    If Not IsMissing(DefaultValue) Then Ctrl.Value = DefaultValue
End Sub

Call on Workbook_Open:调用 Workbook_Open:

Private Sub Workbook_Open()
    Dim Prt As J_Type_Printers: Prt = J_Fx_ListPrinters
    J_Sub_Controls_ComboBox_ListArray List1.PrinterList, Prt.Available, Prt.Default
End Sub

I believe the problem is the declaration of the Source_1D_2D_Array parameter.我相信问题在于 Source_1D_2D_Array 参数的声明。 It is declared as string and it needs to be a string array.它被声明为字符串并且它需要是一个字符串数组。 The type mismatch is because you are passing an string array to a string parameter.类型不匹配是因为您将字符串数组传递给字符串参数。

Also the check for the DefaultValue needs to be a comparison.此外,对 DefaultValue 的检查也需要进行比较。 I think you want:我想你想要:

Sub PopulateComboBoxBy2DArray(Ctrl As Control, ByRef Source_1D_2D_Array() As String, Optional DefaultValue As String = "")
    Ctrl.List = Source_1D_2D_Array
    If DefaultValue<>"" Then
        Ctrl.Value = DefaultValue
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中传递一个用于用户定义函数拟合的数组 - passing an array for user-defined function fitting in python 将数组传递给 VBA 中的 IRR 函数。 不断收到错误“编译错误:类型不匹配:需要数组或用户定义的类型。” - Passing an array into the IRR function in VBA. Keep getting the error "Compile error: Type mismatch: array or user-defined type expected." 在QBasic中的用户定义的TYPE中使用数组 - Use an array in a user-defined TYPE in QBasic 创建用户定义对象类型的数组 - Create an array of user-defined object type VBA函数似乎返回一个数组,但我收到“类型不匹配:预期为数组或用户定义类型” - VBA function seems to return an array but I get “Type mismatch: array or user-defined type expected” 有没有办法在java中制作用户定义的类类型的数组? - Is there a way to make an array of a user-defined class type in java? 如何将新元素添加到用户定义类型的数组? - How to add new elements to an array of a user-defined type? 堆栈或堆栈数组。 用户定义的类型难题 - To heap or stack array. A user-defined type conundrum 在主函数之前声明具有用户定义大小的数组 - Declaring array with user-defined size before the main function 默认参数数组不被馈送到PHP中的用户定义函数中吗? - Default parameters array not being fed into user-defined function in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM