Why can't the VBA Me keyword access private procedures in its own module?

后端 未结 5 2045
梦谈多话
梦谈多话 2020-12-11 15:22

I just discovered that the Me keyword cannot access private procedures even when they are inside its own class model.

Take the following code in Class1:



        
5条回答
  •  心在旅途
    2020-12-11 15:44

    Public Function Fight() As String
    'performs a round of attacks i.e. each character from both sides performs an attack
    'returns a scripted version of the outcomes of the round
    
    'check if buccaneers are all dead
    If mBuccaneers.aliveCount > 0 Then
    
        'check if any hostiles are alive
        If mHostiles.aliveCount > 0 Then
    
            'check we have some buccaneers
            If mBuccaneers.count = 0 Then
                Fight = "There are no buccaneers. Load or create some buccaneers"
            Else
                If mHostiles.count = 0 Then
                    'can't fight
                    Fight = "There are no hostiles to fight. Generate some hostiles"
                Else
                    mScript = ""
                    Call GroupAttack(mBuccaneers, mHostiles)
                    Call GroupAttack(mHostiles, mBuccaneers)
                    Fight = mScript
                End If
            End If
    
        Else 'hostiles are all dead
            Fight = "Hostiles are all dead. Generate a new set of hostiles"
        End If
    
    Else
        Fight = "Buccaneers are all dead :(. Suggest building or loading a new buccaneer group"
    End If
    End Function
    

    Uses the private class method GroupAttack by using the Call statement

    Private Sub GroupAttack(attackersGroup As clsGroup, defendersGroup As clsGroup)
    'implements the attack of one group on another
    
    Dim victimNo As Integer
    Dim randomNumber As Integer
    Dim attacker As clsCharacter
    Dim damage As Integer
    Dim defender As clsCharacter
    Randomize
    
    For Each attacker In attackersGroup.members
    
        'check if attacker is still alive
        If attacker.health > 0 Then
    
            'check if any defenders are still alive because there's no point attacking dead defenders
            If defendersGroup.aliveCount > 0 Then
    
                'do some damage on a defender
                If defendersGroup.count > 0 Then
                    'choose a random hostile
                    victimNo = Int(((Rnd() * defendersGroup.aliveCount) + 1))
    
                    'find an alive victim
                    memberid = 0
                    j = 0
                    Do While j < victimNo
                        memberid = memberid + 1
                        If defendersGroup.members(memberid).health > 0 Then
                            j = j + 1
                        End If
                    Loop
                    'reset our victimno to the live victim
                    victimNo = memberid
    
                    damage = defendersGroup.character(victimNo).attack(attacker.strength)
    
                    If damage <> 0 Then  'attacker hit
                        mScript = mScript & attacker.name & " hits " & _
                        defendersGroup.character(victimNo).name & " for " & damage & " damage"
    
                        If defendersGroup.character(victimNo).health = 0 Then
                            mScript = mScript & " and kills " & defendersGroup.character(victimNo).name
                        End If
                        mScript = mScript & vbCrLf
    
                    Else 'attacker missed
                        mScript = mScript & attacker.name & " missed " & defendersGroup.character(victimNo).name & vbCrLf
                    End If
    
                End If
    
            End If
    
        End If
    
    Next attacker   
    End Sub
    

    Thats all you need to do ,works like a charm

提交回复
热议问题