如何向VB6二维数组中添加元素?
- 内容介绍
- 文章标签
- 相关推荐
本文共计584个文字,预计阅读时间需要3分钟。
使用二维字符串数组存储用户名和密码,可以通过以下步骤将用户添加到数组中:
1. 定义一个二维字符串数组来存储用户名和密码。
2.创建一个函数来添加用户名和密码到数组中。
3.在函数中,将新用户名和密码添加到数组的末尾。
以下是示例代码:
python
定义全局字符串数组users=[[user1, password1], [user2, password2]]定义添加用户的函数def add_user(username, password): # 将新用户名和密码添加到数组末尾 users.append([username, password])
添加新用户add_user(new_user, new_password)
输出用户数组for user in users: print(user)
输出结果将包括所有用户名和密码,包括新添加的用户。
我正在使用2D数组来存储用户的用户名和密码.用户定义为全局字符串数组:Dim users(9, 2) As String
如何将用户添加到该阵列?一次设置用户名和密码.
一旦你对具有显式边界的数组用户进行了Dim,你就无法对它进行ReDim.你可以尝试这个,它不会编译(数组已经标注)
Dim users(9, 2) As String ReDim Preserve users(10, 2) ' doesn't compile!
您将遇到的另一个问题是尝试ReDim保留数组的第一个索引.你也可以尝试,你会得到运行时错误(下标超出范围)
Dim users() As String ReDim Preserve users(0, 1) ReDim Preserve users(1, 1) ' runtime error!
相反,我想出了以下内容.您需要保留第一个索引以区分用户名或密码.这将是1号(而不是2号).随着您添加更多用户,第二个索引将会增加:
Private Sub Form_Load() Dim users() As String ReDim users(1, 0) Add users, "name1", "pw1" Add users, "name2", "pw2" End Sub Private Sub Add(ByRef users() As String, username As String, password As String) If Not (users(0, 0) = vbNullString And users(1, 0) = vbNullString) Then ReDim Preserve users(1, UBound(users, 2) + 1) End If users(0, UBound(users, 2)) = username users(1, UBound(users, 2)) = password End Sub
如果您无法交换用户名和密码,则可能需要查看其他数据结构,例如集合或具有用户名和密码字段的自定义类的数组.
本文共计584个文字,预计阅读时间需要3分钟。
使用二维字符串数组存储用户名和密码,可以通过以下步骤将用户添加到数组中:
1. 定义一个二维字符串数组来存储用户名和密码。
2.创建一个函数来添加用户名和密码到数组中。
3.在函数中,将新用户名和密码添加到数组的末尾。
以下是示例代码:
python
定义全局字符串数组users=[[user1, password1], [user2, password2]]定义添加用户的函数def add_user(username, password): # 将新用户名和密码添加到数组末尾 users.append([username, password])
添加新用户add_user(new_user, new_password)
输出用户数组for user in users: print(user)
输出结果将包括所有用户名和密码,包括新添加的用户。
我正在使用2D数组来存储用户的用户名和密码.用户定义为全局字符串数组:Dim users(9, 2) As String
如何将用户添加到该阵列?一次设置用户名和密码.
一旦你对具有显式边界的数组用户进行了Dim,你就无法对它进行ReDim.你可以尝试这个,它不会编译(数组已经标注)
Dim users(9, 2) As String ReDim Preserve users(10, 2) ' doesn't compile!
您将遇到的另一个问题是尝试ReDim保留数组的第一个索引.你也可以尝试,你会得到运行时错误(下标超出范围)
Dim users() As String ReDim Preserve users(0, 1) ReDim Preserve users(1, 1) ' runtime error!
相反,我想出了以下内容.您需要保留第一个索引以区分用户名或密码.这将是1号(而不是2号).随着您添加更多用户,第二个索引将会增加:
Private Sub Form_Load() Dim users() As String ReDim users(1, 0) Add users, "name1", "pw1" Add users, "name2", "pw2" End Sub Private Sub Add(ByRef users() As String, username As String, password As String) If Not (users(0, 0) = vbNullString And users(1, 0) = vbNullString) Then ReDim Preserve users(1, UBound(users, 2) + 1) End If users(0, UBound(users, 2)) = username users(1, UBound(users, 2)) = password End Sub
如果您无法交换用户名和密码,则可能需要查看其他数据结构,例如集合或具有用户名和密码字段的自定义类的数组.

