Passing arrays with Property Get

Marcelo
09-19-2004, 10:20 PM
from: http://www.vb-helper.com/howto_net_indexed_property.html


In VB .NET, you include the index parameter in the Property statement. In this example, the test_number parameter is the index for the Scores property.


Public Class Student
' The private array of scores.
Private m_Scores(9) As Integer

' The indexed Score property procedures.
Public Property Score(ByVal test_number As Integer) As _
Integer
Get
Return m_Scores(test_number)
End Get
Set(ByVal Value As Integer)
m_Scores(test_number) = Value
End Set
End Property
End Class


It is possible to do that with Visual Basic 6???????

The array has not fixed lenght. Imagine a Student scores along a year.

In my case I have a Student object with many properties (Name, Lastname, Address,...).
What I want to do is to have a read only property called "Scores" and get the entire set of scores stored on Access variable score fields database, using something like this:

Redim MyScores
MyScores ()=Student.Scores

Thanks

Marcelo.

Diurnal
09-19-2004, 11:35 PM
You can build your class with the Class Builder and then change the Property Get statements to arrays after it is updated. Pass the StudentID number to the property like you would a function.

'In a class module called cStudent:
Option Explicit

Private m_Score() As Long


Public Property Get Score(ByVal StudentID As Long) As Long()
'Return an array of scores from a simulated database.

Dim i As Long
Dim lArr() As Long

'Return the data from the database here:
' m_Score() = GetScoreFromDatabase(StudentID)

'Simulation:
ReDim lArr(0 To StudentID)

For i = 0 To StudentID
lArr(i) = 1
Next i

m_Score() = lArr()

Score = m_Score()

End Property


'In the calling form with a textbox and a command button:

Option Explicit

Private Sub Command1_Click()
Dim Student As cStudent
Set Student = New cStudent

Dim lScores() As Long
Dim i As Long
Dim s As String

lScores() = Student.Score(Val(Text1.Text))

For i = 0 To UBound(lScores)
s = s & CStr(lScores(i))
Next i

Text1.Text = s

Set Student = Nothing

End Sub

An alternative is to make a separate student ID property and set that first. Provide a public function in the class to use this ID to return the data.

robertdeniro
09-19-2004, 11:36 PM
The sample you posted lets you change the array directly, but allows you validate input. The code below returns an array directly.
Note that when you return an array this way, you're returning a copy of an array. This can be quite slow for very large arrays; and you're not changing the original value in the source array. To pass it by reference, pass it out via a sub, or function, by reference.

Add a class module called Student, and a module.

Class Student

Option Explicit

Private m_aScores() As Integer


'-------------------------------------------------------------------------
'**returning an array directly from a property
Public Property Get Scores() As Integer()
Scores = m_aScores
End Property

'**form 2 - returning a Variant containing an arrary
Public Property Get vScores() As Variant
vScores = m_aScores
End Property

'-------------------------------------------------------------------------


'-------------------------------------------------------------------------
'**your .Net sample in VB
Public Property Get OneScore(ByVal Index As Long) As Integer
OneScore = m_aScores(Index)
End Property

Public Property Let OneScore(ByVal Index As Long, ByVal NewValue As Integer)
m_aScores(Index) = NewValue
End Property
'-------------------------------------------------------------------------

Private Sub Class_Initialize()
ReDim m_aScores(1 To 4)
End Sub


Module modMain

Option Explicit

Public Sub main()
Dim oStd As New Student
Dim aScores() As Integer
Dim nLoop As Long

'**with an intermediate variable (aScores)
aScores = oStd.Scores
For nLoop = LBound(aScores) To UBound(aScores)
Debug.Print aScores(nLoop)
Next nLoop

'**without an intermediate variable
For nLoop = LBound(oStd.vScores) To UBound(oStd.vScores)
Debug.Print oStd.vScores(nLoop)
Next nLoop

End Sub

Marcelo
09-20-2004, 07:17 PM
Thanks for your solutions and for help me to develope my software.

Marcelo.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum