Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Sunday, June 17, 2012

Membaca File Dan Memasukannya Ke Dalam Array

Option Explicit

Private Sub Command1_Click()

Dim L As Long
Dim MyArray() As String

' Load file into string array
FileToArray "C:\TEST.txt", MyArray

' Reverse array contents
ReverseStrArray MyArray

' show result in immediate window
For L = 0 To UBound(MyArray)
Debug.Print MyArray(L)
Next L

End Sub

Private Sub FileToArray(ByVal sPath As String, ByRef sArray() As String)
Dim ff As Integer
ff = FreeFile
On Error GoTo Fini
Open sPath For Input As #ff
sArray = Split(Input(LOF(ff), ff), vbCrLf)
Fini:
Close #ff
End Sub

Private Sub ReverseStrArray(ByRef sArray() As String)
Dim ubnd As Long, lbnd As Long, x As Long
Dim sTmp As String
ubnd = UBound(sArray)
lbnd = LBound(sArray)
For x = lbnd To ((ubnd - lbnd - 1) \ 2)
sTmp = sArray(lbnd + x)
sArray(lbnd + x) = sArray(ubnd - x)
sArray(ubnd - x) = sTmp
Next x
End Sub
READ MORE - Membaca File Dan Memasukannya Ke Dalam Array

Memperoleh Array Dari Prosedur Fungsi

Option Explicit

Private aiLeftSide() As Integer
Private asLeftSide() As String
Private aiRightSide(1 To 10) As Integer
Private asRightSide(1 To 10) As String
Private obj As Object

Public Function ArrayFromClass() As String()
Dim astr(1 To 10) As String
Dim i As Integer
For i = 1 To 10
astr(i) = "Class array element " & Str(i)
Next i
ArrayFromClass = astr()
End Function

Private Sub Command1_Click()
Dim i As Integer
aiLeftSide = aiRightSide
asLeftSide = asRightSide
For i = 1 To UBound(aiLeftSide)
Debug.Print aiLeftSide(i)
Next i
For i = 1 To UBound(asLeftSide)
Debug.Print asLeftSide(i)
Next i
End Sub

Private Sub Command2_Click()
Dim i As Integer
Dim aInt() As Integer
Dim astr() As String
aInt = ReturnIntArray
astr = ReturnStringArray
For i = 1 To UBound(aInt)
Debug.Print aInt(i)
Next i
For i = 1 To UBound(astr)
Debug.Print astr(i)
Next i
End Sub

Private Sub Command3_Click()
Dim astr() As String
Dim i As Integer
astr = obj.ArrayFromClass
For i = 1 To UBound(astr)
Debug.Print astr(i)
Next i
End Sub

Private Sub Form_Load()
Dim i As Integer
Command1.Caption = "Assign Array"
Command2.Caption = "Call Function that returns Array"
Command3.Caption = "Call Object method that returns Array"
For i = 1 To 10
aiRightSide(i) = i
asRightSide(i) = "This is element " & Str(i)
Next i
Set obj = New Class1
End Sub

Private Function ReturnStringArray() As String()
Dim aString(1 To 10) As String
Dim i As Integer
For i = 1 To UBound(aString)
aString(i) = "Element " & Str(i)
Next i
ReturnStringArray = aString()
End Sub

Private Function ReturnIntArray() As Integer()
Dim aInt(1 To 10) As Integer
Dim i As Integer
For i = 1 To 10
aInt(i) = i
Next i
ReturnIntArray = aInt()
End Sub
READ MORE - Memperoleh Array Dari Prosedur Fungsi

Bagaimana Membuat Function Yang Mengandung Array - VB6

Mengenai cara membuat fungsi yang memperoleh (return) nilai array dalam bahasa pemrograman Visual Basic 6.0 - Untuk membuat fungsi yang mendapatkan nilai array dalam VB6, coba perhatikan bentuk fungsi berikut:
Private Function GetArrFunction() As String()
Dim c(2) As String
c(0) = 1
c(1) = 100
c(2) = 300
GetArrFunction = c
End Function

Private Sub Command2_Click()
Dim s() As String
s = GetArrFunction
MsgBox s(1) 'akan menghasilkan 100
End Sub
Demikianlah contoh sederhana mengenai cara membuat fungsi yang memperoleh (return) nilai array dalam VB6, semoga bermanfaat. nn
READ MORE - Bagaimana Membuat Function Yang Mengandung Array - VB6

Thursday, June 7, 2012

VB6 Code - Meng-copy Array Secara Cepat

Private Declare Sub CopyMemory Lib "kernel32" _ 
Alias "RtlMoveMemory" (Dest As Any, _
Source As Any, ByVal Length As Long)

Private Sub
CopyArray()
Dim lngbytes As Long
Dim
lngSrc(1 To 600000) As Long
Dim
lngDest(1 To 600000) As Long
'
' Number of bytes equals number of array
' elements times the element length.
'
lngbytes = (UBound(lngSrc) - LBound(lngSrc) + 1) * Len(lngSrc(1))
'
' Copy the array passing the address of the start to
' the destination and source arrays and the length
' of the arrays.
'
Call CopyMemory(lngDest(LBound(lngDest)), lngSrc(LBound(lngSrc)), lngbytes)
End Sub
READ MORE - VB6 Code - Meng-copy Array Secara Cepat

Sunday, April 4, 2010

VB6 Code - Memperoleh Nilai Maksimal Dari Sebuah Array

Di bawah ini merupakan fungsi VB6 untuk memperoleh nilai maksimal dari sebuah array.
Option Explicit

Public Function MAX(ByRef Number() As Double) As Double
Dim iMaxNum As Double
Dim i As Integer
iMaxNum = Number(LBound(Number))
For i = LBound(Number) To UBound(Number)
If Number(i) > iMaxNum Then
iMaxNum = Number(i)
Else
iMaxNum = iMaxNum
End If
Next i
MAX = iMaxNum
End Function
Contoh penggunaan fungsi untuk memperoleh nilai maksimal dari sebuah array
Private Sub Command1_Click()
Dim iArray(3) As Double
iArray(0) = 588
iArray(1) = 67
iArray(2) = 66
iArray(3) = 4
MsgBox "The max number is: " & MAX(iArray)
End Sub
READ MORE - VB6 Code - Memperoleh Nilai Maksimal Dari Sebuah Array

VB6 Code - Memperoleh Nilai Minimal Dari Sebuah Array

Di bawah ini merupakan fungsi VB6 untuk mencari nilai minimal dari sebuah array.
Option Explicit

Public Function MIN(ByRef Number() As Double) As Double
Dim iMaxNum As Double
iMaxNum = Number(LBound(Number))
Dim i As Integer
For i = LBound(Number) To UBound(Number)
If Number(i) < iMaxNum Then
iMaxNum = Number(i)
Else
iMaxNum = iMaxNum
End If
Next i
MIN = iMaxNum
End Function
Contoh fungsi untuk memperoleh nilai minimal dari sebuah array
Private Sub Command1_Click()
Dim iArray(3) As Double
iArray(0) = 588
iArray(1) = 67
iArray(2) = 66
iArray(3) = 4
MsgBox "The min number is: " & MIN(iArray)
End Sub
READ MORE - VB6 Code - Memperoleh Nilai Minimal Dari Sebuah Array

VB6 Code - Memperoleh Nilai Rata-rata Dari Sebuah Array

Di bawah ini merupakan fungsi VB6 untuk memperoleh nilai rata-rata dari sebuah array.
Option Explicit

Function AVERAGE(ByRef Number() As Double) As Double
Dim iMaxNum As Double, i As Integer
For i = LBound(Number) To UBound(Number)
iMaxNum = iMaxNum + Number(i)
Next i
AVERAGE = iMaxNum / (UBound(Number) + 1)
End Function
Contoh penggunaan fungsi untuk memperoleh nilai rata-rata dari sebuah array
Private Sub Command1_Click()
Dim iArray(3) As Double
iArray(0) = 588
iArray(1) = 67
iArray(2) = 66
iArray(3) = 4
MsgBox "The Average is: " & AVERAGE(iArray)
End Sub
READ MORE - VB6 Code - Memperoleh Nilai Rata-rata Dari Sebuah Array