Home > Software engineering >  Help to VB dynamic array subscript cross-border issues
Help to VB dynamic array subscript cross-border issues

Time:10-08

 Private Sub Command1_Click () 
Dim strLen As Integer
Dim HLSTR As String
Dim dataMe As Integer
Dim mytext1 As String
Dim nbyte () As Byte
Dim As Integer I
Dim endchr As Integer

StrLen=Len (Text1. Text)
Mytext1=Left (Text1. Text, Len (Text1. Text) - 2)
I=0
For n=1 To Len (mytext1)
N=n - 1
If n Mod 3=0 Then
HLSTR=Mid (mytext1, 1, 2)
Nbyte (I)=Right (HLSTR, 1) + Left (HLSTR, 1) * 16
I=I + 1
N=n + 3
End the If
The Next n
Endchr=UBound (nbyte)
Endchr=endchr + 1
Nbyte (endchr)=& amp; HA
MSComm1. The Output=nbyte

End Sub

Display nbyte subscript crossing the line is what reason, hope everybody action ~ ~

CodePudding user response:

Endchr=UBound (nbyte)=="endchr at this time is the last element of nbyte
Endchr + 1===endchr & gt; + 1 again, don't you out just strange!
Nbyte (endchr)=& amp; HA

CodePudding user response:

"Subscript bounds" error of the building Lord, is the first time appeared in the for loop, the nbyte (I) assignment of that!
Because nbyte () also did not assign data space, the array is empty,
Before the for loop, it should be according to the value of the strlen with Redim () statement allocation of space to size,
The second is the upstairs said,

In addition, the building of a for loop that code, there are logic errors,
After correction "subscript bounds" problem, also can't get the correct "transformation results,"
The for loop "usage", is obviously unreasonable,

CodePudding user response:

Logical comparison mess,

1 in a loop, and the loop variable or minus or plus, actually can use looping statements to complete:

For n=0 To Len Step 3 (mytext1) - 1

2 nbyte instantiation they need before using, namely allocate space

According to your process logic, can be in a loop Redim Preserve nbyte (I)

3 application processing problems

HLSTR=Mid (mytext1, 1, 2)
Nbyte (I)=Right (HLSTR, 1) + Left (HLSTR, 1) * 16

Nbyte all the elements, to be the same content,

4 remove the three sentences below

Endchr=UBound (nbyte)
Endchr=endchr + 1
Nbyte (endchr)=& amp; HA

Instead of
Redim Preserve nbyte (Ubound (nbyte) + 1)
Nbyte (Ubound (nbyte))=& amp; HA

The original poster to strengthen the training of the logical thinking, which cannot adapt to programming,

CodePudding user response:

Thank you for your patience solutions, graduation design should be written in VB and Siemens PLC serial communication program, write to write to the problems, I will continue to try, as you suggest thanks again

CodePudding user response:

Dim nbyte () As Byte
Just define a dynamic array, but still not allocate memory array
You have to use redim first before use to set the length of the array.

CodePudding user response:

I also met the same problem, and the building of the above answer also didn't understand, please answer!!!!!!!!!!!!!!!!

 
Dim I As an Integer, j As an Integer, s As Integer
Dim szpxin (5, 6) As a Double, sznxin (5, 6) As a Double
Dim szn1 (5, 6) As a Double, szsigmas (5, 6) As a Double, SZWM (5, 6) As a Double
Dim sztiao (5, 6) As a Double, szdaq (5, 6) As a Double
Dim SZN As the Variant
Dim SZD As the Variant
SZN=Array (4, 5, 6, 7, 8, 9)
SZD=Array (20, 22, 25, 28, 32, 36, 40)
'initialize Array way
Dim rpxin (29) As a Double, rnxin (29) As a Double
Dim rn1 (29) As a Double, rsigma (29) As a Double, RWM (29) As a Double
Dim rtiao (29) As a Double, rdaq (29) As a Double
Dim rn (40) As a Double, rd (29) As a Double

For I=0 To 5
For j=0 To 6
SZD Call pxin (SZN (I), (j), szpxin (I, j), sznxin (I, j))
SZD Call NNN (SZN (I), (j), szn1 (I, j), szsigmas (I, j), SZWM (I, j), sztiao (I, j), szdaq (I, j))
Next j
Next I

S=0
For I=0 To 5
For j=0 To 6
If SZWM (I, j) & lt;=0.25 Then
Rn (s)=SZN (I)
Rd (s)=SZD (j)
Rpxin (s)=Int (szpxin (I, j) * 10000)/10000
Rnxin (s)=Int (sznxin (I, j) * 100000)/100000
Rn1 (s)=Int (szn1 (I, j) * 100000)/100000
Rsigma (s)=Int (szsigmas (I, j) * 1000)/1000
RWM (s)=Int (SZWM (I, j) * 10000)/10000
Rtiao (s)=Int (sztiao (I, j) * 10)/10
Rdaq (s)=Int (szdaq (I, j) * 1000 * 10000)/10000
S=s + 1
End the If
Next j
Next I


Program you use function calculation, first by different combination of (n, d) to calculate the range of parameter values, and then extracted from the calculation results meet the requirements of value

But in the cycle of the extraction results, I clearly defines the s=0, before the runtime does suggest the subscript crossing the line, I put the mouse on the code found the value of s is 30; S=s + 1 is in meet the condition will only be executed, so s to 30 entirely unscientific,

Are scratching their heads, and don't know where I went wrong, solve! Genuflect is begged!!!!!!!!!!!!!!!!!!! Thank you very much

CodePudding user response:


Where did I find the problem,,,, sorry o (╯/╰) o
Ignore me,,,, and when to give the building Lord added popularity,,
  • Related