返回数组的下界和上界

xingnup 2003-02-15 12:18:19
当一个动态数组未设置上界和下界时,不能用LBound()及UBound()函数来返回数组的下界和上界,有什么方法可以解决这个问题呢?
...全文
491 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Corny 2003-02-19
  • 打赏
  • 举报
回复
未初始化的数组哪里来上下界,只不过具有一个变量标识而已,甚至内存位置都没有确定

怪怪怪.......
xingnup 2003-02-18
  • 打赏
  • 举报
回复
误会了!我说不能用Lbound()及UBound()是说一个数组未初始化时,即尚未执行Redim(intValue)时。
lxcc 2003-02-18
  • 打赏
  • 举报
回复
当然不能用!
用户 昵称 2003-02-15
  • 打赏
  • 举报
回复
notice this

Dynamic arrays are great tools for creating a collection of values
on the fly. However, you may have run into trouble when trying to
determine if the array contains any items. The UBound() function
provides one way to tell if it does. However, if the array doesn't
officially exist yet--that is, you haven't redimensioned it with
even a single item--then this function generates an error.

To accommodate this problem, some people suggest automatically
redimensioning the array with its first item, for instance:

ReDim strCats(0)

However, why have Visual Basic create the array if it potentially may
not use it? Another often-suggested alternative is to use a counter
variable; something along the lines of:

MyUpper = 0

Sub AddValue(strValue as String)
ReDim Preserve strCats(MyUpper)
strCats(MyUpper) = strValue
MyUpper = MyUpper + 1
End Sub

While this method works, the counter technique can get messy in large
applications; not to mention redundant. After all, Visual Basic already
keeps track of the current upper boundary, so it's much more efficient
to let it do the job for you.

As a more efficient method, try simply trapping for the error that the
UBound() function throws when the array doesn't contain any items
(Error 9, subscript out of range), as in:

Private Function GetUpper(varArray As Variant) As Integer
Dim Upper As Integer
On Error Resume Next
Upper = UBound(varArray)
If Err.Number Then
If Err.Number = 9 Then
Upper = 0
Else
With Err
MsgBox "Error:" & .Number & "-" & .Description
End With
Exit Function
End If
Else
Upper = UBound(varArray) + 1
End If
On Error GoTo 0
GetUpper = Upper
End Function
用户 昵称 2003-02-15
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Dim a()
Debug.Print isarraydimed(a)
ReDim a(10)
Debug.Print isarraydimed(a)
End Sub

Private Function isarraydimed(a() As Variant) As Boolean
On Error GoTo errhandle
Debug.Print UBound(a)
isarraydimed = True
Exit Function
errhandle:
isarraydimed = False
Err.Clear
End Function
Corny 2003-02-15
  • 打赏
  • 举报
回复
谁说的动态数组不可以呀

你试试再问嘛
w18ily 2003-02-15
  • 打赏
  • 举报
回复
LBound函数 返回

返回一个 Long 型数据,其值为指定数组维可用的最小下标。

语法

LBound(arrayname[, dimension])

LBound 函数的语法包含下面部分:

部分 描述
arrayname 必需的。数组变量的名称,遵循标准的变量命名约定。
dimension 可选的;Variant (Long)。指定返回哪一维的下界。1 表示第一维,2 表示第二维,以此类推。如果省略 dimension,就认为是 1。


说明

LBound 函数与 UBound 函数一起使用,用来确定一个数组的大小。UBound 用来确定数组某一维的上界。

对具有下述维数的数组而言,LBound 的返回值见下表:

Dim A(1 To 100, 0 To 3, -3 To 4)

语句 返回值
LBound(A, 1) 1
LBound(A, 2) 0
LBound(A, 3) -3


所有维的缺省下界都是 0 或 1,这取决于 Option Base 语句的设置。使用 Array 函数创建的数组的下界为 0;它不受 Option Base 的影响。

对于那些在 Dim 中用 To 子句来设定维数的数组而言,Private、Public、ReDim 或 Static 语句可以用任何整数作为下界。
13161795500 2003-02-15
  • 打赏
  • 举报
回复
up
w18ily 2003-02-15
  • 打赏
  • 举报
回复
UBound函数 返回

返回一个 Long 型数据,其值为指定的数组维可用的最大下标。

语法

UBound(arrayname[, dimension])

UBound 函数的语法包含下面部分:

部分 描述
arrayname 必需的。数组变量的名称,遵循标准变量命名约定。
dimension 可选的;Variant (Long)。指定返回哪一维的上界。1 表示第一维,2 表示第二维,以此类推。如果省略 dimension,就认为是 1。


说明

UBound 函数与 LBound 函数一起使用,用来确定一个数组的大小。LBound 用来确定数组某一维的上界。

对具有下述维数的数组而言,UBound 的返回值见下表:

Dim A(1 To 100, 0 To 3, -3 To 4)

语句 返回值
UBound(A, 1) 100
UBound(A, 2) 3
UBound(A, 3) 4
YAOHE 2003-02-15
  • 打赏
  • 举报
回复
楼上的说得对可以!完全可以!
ccbl 2003-02-15
  • 打赏
  • 举报
回复
ubound()
lbound()

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧