简体   繁体   English

在Visual Basic中声明数组大小

[英]Declaring array size in Visual Basic

This is my first time working with VB. 这是我第一次与VB合作。 I'm mostly used to working in matlab and am finding that a lot of what I take for granted in matlab must be explicitly declared in VB. 我最习惯于在matlab中工作,发现在matlab中我想当然的很多东西必须在VB中明确声明。 Frustrating! 令人沮丧!

In particular I need to declare two array sizes s1 and s2. 特别是我需要声明两个数组大小s1和s2。 As you can see in the code below, s1 is the number of elements in set "n" where the variable "stratum" equals 1. s2 is the number of elements in set "n" where the variable "stratum" equals 2. Pretty straightforward. 从下面的代码中可以看到,s1是变量“ stratum”等于1的集合“ n”中的元素数。s2是变量“ stratum”等于2的集合“ n”中的元素数。直截了当。

My approach is simply to loop the stratum variable 1 through n and count these occurences; 我的方法是简单地循环变量1到n并计算这些事件的发生。 and then declare the resulting sums as constants. 然后将所得的和声明为常量。 This would work swimmingly in matlab, but VB is not accepting s1 and s2 as constants. 这在matlab中可以正常工作,但是VB不接受s1和s2作为常量。 It won't even display s1 and s2 when I insert a debug.print command after the loop. 当我在循环之后插入debug.print命令时,它甚至不会显示s1和s2。

I have looked through the relevant posts already made. 我已经浏览了已经发表的相关帖子。 I appreciate any advice. 我感谢任何建议。 Thanks. 谢谢。

    Sub TOAinput()

    Const n As Integer = 648

    Dim stratum(n), hybrid(n), acres(n), hhsz(n), offinc(n)

    For i = 1 To n
        stratum(i) = Worksheets("hhid level").Cells(i + 1, 2).Value
    Next i

    Dim s1 As Integer
    Dim s2 As Integer


    s1 = 0
    s2 = 0
    For i = 1 To n
        If stratum(i) = 1 Then
            s1 = s1 + 1
        Else:
            s2 = s2 + 1
        End If
    Next i

    Dim acres1(s1), hhsz1(s1), offinc1(s1), acres2(s2), hhsz2(s2), offinc2(s2)

    (...)
    End Sub

A couple of comments: 几点评论:

If you're changing the values of s1 and s2 in your code (as you are in your for loop) they should not be declated as Const. 如果您要在代码中更改s1和s2的值(就像在for循环中一样),则不应将它们定义为Const。 You also appear to be trying to use s1 and s2 before they have been declared. 您似乎还试图在声明它们之前使用s1和s2。

I'm not sure what exactly you are trying to do with the "Const s1 As Integer = s1" statements. 我不确定您到底想使用“ Const s1 As Integer = s1”语句做什么。 You probaly just want "Dim s1 as Integer" and "Dim s2 as Integer" near the top of your code. 您可能只希望在代码顶部附近添加“将Dim s1作为整数”和“将Dim s2作为整数”。

This should work slightly better for you: 这应该对您更好一些:

Sub TOAinput()

Const n As Integer = 648

Dim stratum(n) As Integer
Dim hybrid(n) As Integer
Dim acres(n) As Integer
Dim hhsz(n) As Integer
Dim offinc(n) As Integer

Dim i As Integer

For i = 1 To n
    stratum(i) = Worksheets("hhid level").Cells(i + 1, 2).Value
Next

Dim s1 As Integer
Dim s2 As Integer

For i = 1 To n
    If stratum(i) = 1 Then
        s1 = s1 + 1
    Else
        s2 = s2 + 1
    End If
Next

Dim acres1() As Integer
Dim hhsz1() As Integer
Dim offinc1() As Integer
Dim acres2() As Integer
Dim hhsz2() As Integer
Dim offinc2() As Integer

ReDim acres1(s1)
ReDim hhsz1(s1)
ReDim offinc1(s1)
ReDim acres2(s2)
ReDim hhsz2(s2)
ReDim offinc2(s2)

(...)
End Sub

I would also recommend that, if possible, you declare the types on the arrays. 我还建议,如果可能,请在数组上声明类型。 I have assumed that they are all ints and modified the code appropriately, but this may not work in your situation (ie different data types). 我假设它们都是整数,并已适当地修改了代码,但这可能不适用于您的情况(即不同的数据类型)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM