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

后端 未结 5 1993
梦谈多话
梦谈多话 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:39

    In COM, there's a difference between the types of object instances and the types of object variables. In particular, the types of object variables behave as interface types. Every type implements at least one interface (itself), but types may implement other interfaces as well. Such ability is used to fake inheritance.

    In some frameworks, if class Foo has a private member Bar, then any non-null variable of type Foo will hold a reference to some class object which contains that member. The member may not be accessible to any outside code, but it will exist, and can thus be accessed from anywhere within the code for Foo.

    Because COM class-variable types behave like interfaces rather than inheritable class types, however, there's no guarantee that a variable of type Foo will refer to an object which has any of Foo's non-public members. While a compiler could know that Me will always refer to the present object, which will be of actual type Foo, the fact that the only object upon which a private member of Foo could be accessed is Me means that there's no real reason for the compiler to support dot-based dereferencing of private members.

    0 讨论(0)
  • 2020-12-11 15:40

    Any guess as to why it was designed that way would be pure supposition without talking to the designers. But my own guess is this, the Me keyword returns a reference to the object the code is currently executing in. I would guess rather than create a special case for Me, they found it easier to continue to obey rules of scope for an object. Which is to say object.method can only work on public or friend methods. So Me, is exactly what it says, an instance of the currently executing object. And since VBA/VB6 doesn't have shared methods, it doesn't really matter if you prefix with Me or not.

    But if it makes you feel any better, I find it incredibly obnoxious too.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-11 15:46

    You do not need the Me keyword to call inside own class.

    0 讨论(0)
  • 2020-12-11 15:47

    Me is this class object instance. So no one can directly call private subs or functions or access private variables except this class public functions or subs.

    0 讨论(0)
提交回复
热议问题