Home > Software engineering >  VB how to judge matrix array is empty
VB how to judge matrix array is empty

Time:09-17

In the following this simple code, for example:

Dim f () as a double
Dim h () as a double

Private Sub Command1_Click () ', 'this is a computational process
Redim f (2, 2)
Redim h (2, 2)
"' comes from an excel f
H=f
End sub

Private Sub Command2_Click ()
If h is an empty array then
Msgbox "error: did not calculate, please recalculate"
The else
H into another form
End the if
End sub

Excuse me each great god , who know how to realize the function of Command2, main problem of determining an empty array,

CodePudding user response:

 
Private Sub Command1_Click ()
Dim (f) As String
If CheckNull (f) Then MsgBox "empty" Else MsgBox "not empty"
ReDim f (1)
If CheckNull (f) Then MsgBox "empty" Else MsgBox "not empty"
ReDim f (1, 1)
If CheckNull (f) Then MsgBox "empty" Else MsgBox "not empty"
Erase f
If CheckNull (f) Then MsgBox "empty" Else MsgBox "not empty"
End Sub
'check empty array
Private Function CheckNull (aryIn As the Variant) As Boolean
On Error GoTo errhandle
Dim As Long a
A=LBound (aryIn)
CheckNull=False
The Exit Function
Errhandle:
CheckNull=True
End the Function

CodePudding user response:

 
Private Declare Function VarPtr Lib "msvbvm60. DLL" (ByRef Ptr () As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any Source, As Any, ByVal Length As Long)
The Public Function IsEmptyArray (Arr () As Double) As Boolean
Dim the Result As Long
CopyMemory Result, ByVal VarPtr (Arr), 4
IsEmptyArray=CBool (Result=0)
End the Function

CodePudding user response:

To determine whether a dynamic array initialization
Dim MyArray on () As String, NumberOfElements As Long

If (Not MyArray on)=1 Then
NumberOfElements=0 'The array is uninitialised
The Else
NumberOfElements=UBound (MyArray on) + 1
End the If

CodePudding user response:

With the API:
'API to determine the array is empty or not initialized
Private Declare Function SafeArrayGetDim Lib "oleaut32. DLL" (ByRef saArray () As Any) As Long
The value of 0 indicates an uninitialized

CodePudding user response:

The
reference 3 floor zzyong00 response:
to determine whether a dynamic array initialization
Dim MyArray on () As String, NumberOfElements As Long

If (Not MyArray on)=1 Then
NumberOfElements=0 'The array is uninitialised
The Else
NumberOfElements=UBound (MyArray on) + 1
End the If


Before learning. Have been using the API. This seems very good... From discovering the new continent..

CodePudding user response:

 Private Sub Command2_Click () 
Dim I, j, k, num As Integer 'I, j, k is the intermediate variable, num is to determine the size of the matrix (a few specific *)
Num=UBound (a) * UBound (a, 2)
For I=0 To UBound (a) - 1 'starting from 1 d
For j=0 To UBound (a, 2) - 1 '2 dimension To judge
If a (I, j)="" Then k=k + 1 'record the number of empty
Next
Next
If k=num Then is' judgment matrix is null
Msgbox "error: did not calculate, please recalculate" : the exit sub
The else
'
h into another form
End sub
[/code]

CodePudding user response:

refer to 6th floor qq_42927700 response:
 Private Sub Command2_Click () 
Dim I, j, k, num As Integer 'I, j, k is the intermediate variable, num is to determine the size of the matrix (a few specific *)
Num=UBound (a) * UBound (a, 2)
For I=0 To UBound (a) - 1 'starting from 1 d
For j=0 To UBound (a, 2) - 1 '2 dimension To judge
If a (I, j)="" Then k=k + 1 'record the number of empty
Next
Next
If k=num Then is' judgment matrix is null
Msgbox "error: did not calculate, please recalculate" : the exit sub
The else
'
h into another form
End sub
[/code]

You dig a grave,
When the array is empty UBound return value may be 1, may be an error,
This code or you have any question,

CodePudding user response:

In setting a judgment with the location of the array,

CodePudding user response:

refer to the eighth floor zhanghaaaa response:
set in an array with the location of a judgment,

You still read a few years to say again...

  • Related