符号定义不同 VB

Symbol already defined differently VB

我正在尝试编译以下代码,但一直出现错误。我之前多次遇到这个错误,所以我被迫使用变通方法。这次我真的厌倦了这个问题,我需要知道这里出了什么问题。

sub SQL_AddTestResults (byval sData as string, byval testID as integer)

   dim i as integer
   dim dataChain as string
   dim aData (Split(sData, ";").length) as string

   aData = Split(sData, ";")

   for i = 0 to aData.Length

      if(i = 4) then
          goto skip
      elseif (i = 68) then
          goto skip
      elseif (i = 72) then
          goto skip
      end if

      if(i = aData.length) then
          dataChain = dataChain & aData(i)
      else
          dataChain = dataChain & aData(i) & ", "
      end if

      skip:
   next

   MsgBox (dataChain)

   SQL_statement = "INSERT INTO ""TestData"" VALUES (" & dataChain & ");"   
   Stmt = connection.createStatement()
   Stmt.executeUpdate(SQL_statement)
end sub 

编译此代码在 "for i = 0 to aData.Length" 行出现以下错误:

基本语法错误。

符号 aData 已经有不同的定义。

不知道为什么。如果这是一个微不足道的问题,我深表歉意,但我对 VB 完全陌生。 C++ 没让我为此做好准备。

经典 VB 中的数组没有 "length" 属性。我不确定你从哪里得到的。

在经典 VB 中获取数组边界的方法是使用 LBound 和 UBound 函数。

for i = LBound(aData) to UBound(aData)

这样你甚至可以处理没有 0 作为起始索引的数组,是的,VB 的一个奇妙的怪癖是它允许你使用任何范围的数字作为你的索引.

VB6 不是我推荐用于新开发的语言。如果您想学习新东西,还有很多其他选择。毫无疑问,您已经注意到,越来越难找到关于经典 VB 如何做事,以及它与 VBScript 和 VB.NET 有何不同的文档。如果您需要维护较旧的 VB6 代码库,我建议您找一本介绍 VB6 语法和用法的旧书。

据我了解,您定义了两次 aData,但方式不同 -

dim aData (Split(sData, ";").length) as string

aData = Split(sData, ";")

aData 长度将 return 一个实际长度的整数,而你要求它 return 一个字符串,并且你在你的整数循环中使用它作为计数器。

在那之后,您立即告诉它 return 一些导致崩溃的数据。而是使用另一个提名人来保存您需要的两种不同类型的 returned 信息 -

dim aData (Split(sData, ";").length) as Long ''Rather use long as the length might exceed the integer type. Use the same for i, change integer to long

Dim bData = Split(sData, ";") as String

for i = 0 to aData.Length

  if(i = 4) then
      goto skip
  elseif (i = 68) then
      goto skip
  elseif (i = 72) then
      goto skip
  end if

  if(i = aData.length) then
      dataChain = dataChain & bData(i)
  else
      dataChain = dataChain & bData(i) & ", "
  end if

  skip:
next

试试这个代码更正后的代码:

sub SQL_AddTestResults (byval sData as string, byval testID as integer)
dim i as integer
dim dataChain as string
dim aData as variant

aData = Split(sData, ";")

for i = 0 to ubound(aData)

  if(i = 4) then
      goto skip
  elseif (i = 68) then
      goto skip
  elseif (i = 72) then
      goto skip
  end if

  if(i = ubound(aData)) then
      dataChain = dataChain & aData(i)
  else
      dataChain = dataChain & aData(i) & ", "
  end if

  skip:
  next
  MsgBox (dataChain)

  SQL_statement = "INSERT INTO ""TestData"" VALUES (" & dataChain & ");"   
  Stmt = connection.createStatement()
  Stmt.executeUpdate(SQL_statement)
end sub