Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21205

array function - > Redim preserve and array copy --variable used as automation type

$
0
0
I wanted to write some function in vb6 for 1D array like "linespace" that creat a 1D array and fill with values starting from x to y with increment n

2) and a 2D array function "zeros" that creat a 2D array and fill all elements(i j) with zeros(0). I added a MsFlexGrid and a listbox and a command buttons.

Now if I dont add "Option Explicit", and "Dim arr() As Variant" then the function for 1D works and also it list the elements in listbox But function of 2D has peoblem and listing in flexgrid dos not work and gives error ---> type mismatch.

On the other hand if I add "Option Explicit", and "Dim arr() As Variant" then the function for 2D works and also it list the elements in flexgrid But then function of 1D and listing does not work and gives error --> variable used as automation type not supported in visual basic

I have to use all of them in smae program and also with Option explicit . How can I do that



' Option Explicit
' Dim arr() As Variant


' ############################
Dim a() As Double
Dim j As Integer
Dim i As Integer

Function linespace(arr As Variant, x As Double, y As Double, n As Integer) As Variant

Dim i As Long, j As Long, result As Variant
Dim m As Double
Dim xx As Double
' First check whether we can really work with this array.
m = n - 1

For i = 0 To m

ReDim Preserve a(i) As Double
xx = x + i * ((y - x) / m)
a(i) = xx
Next i

arr = a()

End Function


Private Sub list1D_Click()

For i = LBound(a()) To UBound(a())
List1.AddItem a(i)
Next

End Sub

###########################
Private Sub zeros(arr As Variant, m As Integer, n As Integer)

Dim r As Integer
Dim c As Integer

ReDim arr(1 To m, 1 To n)
For r = LBound(arr, 1) To UBound(arr, 1)
For c = LBound(arr, 2) To UBound(arr, 2)
arr(r, c) = 0
Next c
Next r
End Sub

Private Sub CopyArrayToGrid(arr As Variant, grd As MSFlexGrid)

Dim rmin As Integer
Dim cmin As Integer
Dim rmax As Integer
Dim cmax As Integer
Dim r As Integer
Dim c As Integer

rmin = LBound(arr, 1)
rmax = UBound(arr, 1)
cmin = LBound(arr, 2)
cmax = UBound(arr, 2)

grd.Rows = rmax - rmin + 1
grd.Cols = cmax - cmin + 1
grd.FixedCols = 0
grd.FixedRows = 0

For r = rmin To rmax
For c = cmin To cmax
grd.TextMatrix(r - rmin, c - cmin) = arr(r, c)
Next c
Next r
End Sub
Attached Files

Viewing all articles
Browse latest Browse all 21205


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>