Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB6 For循环在一个函数中不被接受,在另一个函数中工作正常_Vb6_Foreach_Activex - Fatal编程技术网

VB6 For循环在一个函数中不被接受,在另一个函数中工作正常

VB6 For循环在一个函数中不被接受,在另一个函数中工作正常,vb6,foreach,activex,Vb6,Foreach,Activex,我有一个用VB6编写的旧ActiveX组件要支持(甚至不用麻烦要求它现代化,这正是我目前拥有的),它在编译以下代码时会做一些奇怪的事情: Dim connectedPrinter As printer Dim printers() As String For Each connectedPrinter In printers printers(UBound(printers)) = connectedPrinter.DeviceName Next 它所要做的就是列出所有连接的打印机。

我有一个用VB6编写的旧ActiveX组件要支持(甚至不用麻烦要求它现代化,这正是我目前拥有的),它在编译以下代码时会做一些奇怪的事情:

Dim connectedPrinter As printer
Dim printers() As String

For Each connectedPrinter In printers
    printers(UBound(printers)) = connectedPrinter.DeviceName
Next
它所要做的就是列出所有连接的打印机。但是,在编译时,VB6告诉我

数组上的每个控制变量都必须是变量

奇怪的是,在相同的代码库的另一个函数中,我为不同的任务使用完全相同的循环(设置当前打印机默认值)

然而,它毫不犹豫地被接受,编译并在生产环境中运行


这是怎么回事?

在问题片段中,您有一个名为
printers
的本地数组,它隐藏了
printers
集合。您可以重命名本地数组,或者通过将其引用为
VB来限定对集合的访问。打印机

这样一个愚蠢的错误。。。我想这是真的说“看不见树木的树木”。。。谢谢树木不见森林,局部不见整体。
Dim pPrinter As printer

For Each pPrinter In printers
    If (pPrinter.DeviceName = sPrinterName) Then
        Set printer = pPrinter
        Exit For
    End If
Next