realbasic-nug
[Top] [All Lists]

Re: Scope/Protection Question

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Scope/Protection Question
From: William Squires <wsquires@satx.rr.com>
Date: Sat, 30 May 2009 13:47:16 -0500
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <345AFE42-338C-4588-9F76-7560C88B8609@verizon.net>
References: <345AFE42-338C-4588-9F76-7560C88B8609@verizon.net>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
A protected property can be accessed via code in IT'S instance, or in code in a subclass method (which would refer to its inherited property), but not in other code.

That is, in the code in Class2, you can refer to Property1 without an accessor, but you'll be referring to the value of Property1 associated with the instance of Class2 that you created with New. A different instance of Class2 cannot access the Property1 in the first instance without an accessor!!

I.e.

Public Class Class1 Inherits Object
  Protected Property1 As String

  Public Sub Constructor()
    Property1 = "Hi, I'm an instance of Class1"
  End Sub

  Public Sub Foo()
    MsgBox "Property1 = " + Property1
  End Sub
End Class

Public Class Class2 Inherits Class1
  Protected Property Parent As Class1

  Public Sub Constructor(p As Class1)
    Parent = p

// Allowed as Class2 IsA Class1, and so inherits a protected Property1!
    Property1 = "Hi, I'm an instance of Class2"
  End Sub

  Public Sub Bar()
    // This is allowed
    MsgBox "Property1 = " + <typecast>(Property1)
  End Sub
End Class

Given the above:

Dim a As Class1
Dim b As Class2

a = New Class1()
a.Foo() // Obviously OK, because Class1 owns Property1
b = New Class2(a)
b.Bar() // Also allowed as B IsA A, and so has a Property1 also.
b.Parent.Property1 = "This is bad!" // Not allowed because this code is 'outside' of Class1, and // cannot see into Class1's protected or private instance variables! // Also not allowed because you can't see 'Parent' from outside methods
                                    // of Class2!

Even it I go:

Public Sub Bas()
  Parent.Property1 = "This is also bad!"
End Sub

and I put this method in Class2, I expect a compiler error as "Parent" is an reference to an object of Class1, but from there you can't see anything but Public Properties or Methods, and Property1 is not one of them!

On May 30, 2009, at 1:08 AM, Karen wrote:

Say I have a Class1 that has a PROTECTED property Property1

Class2 Inherits From Class1
Class2 Has a property Parent as Class1

In a method on Class1 I have

me.Property1 = Parent.Property1

I thought I should be able to do that as i thought protected properties should be able accessed within the class and it's subclasses

But the compiler said:
This property is protected. It can only be used from within its class, isSelectedMy = Parent.isSelectedMy


I thought I had done that before. Is this a bug or is it just too late at night and I am tired and confused?

RB2009r2.1 Mac OSX 10.4.11 PPC

Thanks,
- Karen


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>