0

O need a little help with custom classes in VBA...


I do have two classes First one - shift

'.... class k_s
'.... 
Private pNav As Double
''********************************************** NAV
Public Property Get nav_val() As Double
nav_val = pNav
End Property
Public Property Set nav_val() As Double
nav_val = pNav
End Property

Next class is a plan - will contain the array of the class above:

'...class cPlan
'************* ATTR
Private plan() As k_s
'********* Add - this method is called from the master form to populate the array
Public Sub add_s(pol As k_s)
ReDim Preserve plan(UBond(plan) + 1)
Set plan(UBond(plan)) = pol
End Sub

'------ sorting method Public Sub seradit_polozky()

QSort plan(), 0, UBound(pole) 'serazeny =True End Sub


I would like to adjust the sorting sub of smink so i can se it in the cPlan method to sort my objects (shifts) any time inside.

'********** VBA array sort function?

Private Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)

  Dim pivot   As Variant
  Dim tmpSwap As Variant
  Dim tmpLow  As Long
  Dim tmpHi   As Long

  tmpLow = inLow
  tmpHi = inHi

  pivot = vArray((inLow + inHi) \ 2)

  While (tmpLow <= tmpHi)

 While (vArray(tmpLow) < pivot And tmpLow < inHi)
    tmpLow = tmpLow + 1
 Wend

 While (pivot < vArray(tmpHi) And tmpHi > inLow)
    tmpHi = tmpHi - 1
 Wend

 If (tmpLow <= tmpHi) Then
    tmpSwap = vArray(tmpLow)
    vArray(tmpLow) = vArray(tmpHi)
    vArray(tmpHi) = tmpSwap
    tmpLow = tmpLow + 1
    tmpHi = tmpHi - 1
 End If

  Wend

  If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
  If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi

End Sub

this Sub, so it can sort my objects(k_s) inside the array of (class cPlan) based on nav_val property of the objects inside.

I tried fixing the definition of the function, by adding .nav_val to vArray(xxx).nav_val, pivot.nva_val but I'm error "91 Object variable or WIth block variable not set".

Have you tried to solve similar problem?

Community
  • 1
  • 1
EvanS
  • 1
  • 1
  • What line throws the error? (Please make sure to set your options to "Break in class module") Also, please see this [FAQ](http://stackoverflow.com/help/mcve) and [this one](http://stackoverflow.com/help/how-to-ask). I'd like to help you, because I think it could be a very interesting question, but I really don't understand what you're asking. – RubberDuck May 16 '14 at 20:50
  • Hello. Yes, right, I will redefine the question after I get some sleep. – EvanS May 17 '14 at 21:48

1 Answers1

0

I know this question is really old, but i gonna answer it anyway. Your Problem is that you don't use set when using the class fields.

Anyway here is a complete dynamic quicksort Version with the field Name as Parameter:

 Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long, field As String)

Dim pivot   As Variant
Dim tmpSwap As Person
Dim tmpLow  As Long
Dim tmpHi   As Long

tmpLow = inLow
tmpHi = inHi

pivot = CallByName(vArray((inLow + inHi) \ 2), field, VbGet)

While (tmpLow <= tmpHi)

 While (CallByName(vArray(tmpLow), field, VbGet) < pivot And tmpLow < inHi)
    tmpLow = tmpLow + 1
 Wend

 While (pivot < CallByName(vArray(tmpHi), field, VbGet) And tmpHi > inLow)
    tmpHi = tmpHi - 1
 Wend

 If (tmpLow <= tmpHi) Then
    Set tmpSwap = vArray(tmpLow)
    Set vArray(tmpLow) = vArray(tmpHi)
    Set vArray(tmpHi) = tmpSwap
    tmpLow = tmpLow + 1
    tmpHi = tmpHi - 1
 End If

Wend

If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi, field
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi, field

End Sub
Doktor OSwaldo
  • 5,732
  • 20
  • 41