New topic: Class tree object coding?
<http://forums.realsoftware.com/viewtopic.php?t=47462> Page 1 of 1 [ 6 posts ] Previous topic | Next topic Author Message tseyfarth Post subject: Class tree object coding?Posted: Sun Mar 31, 2013 8:40 pm Joined: Sat Dec 04, 2010 9:14 pm Posts: 876 Hello all, The question is a bit unclear, because I am unclear how to describe my question! I have the following program structure tree: App Module Property MainClassA As clsMainClass Property MainClassB As clsMainClas Property MainClassC As clsMainClas Method (cls As String) If cls = "A" Then MainClassA = New clsMainClass Elseif cls = "B" Then MainClassB = New clsMainClass ElseIf cls = "C" Then MainClassC = New clsMainClass End If {within clsMainClass} Property ABC() As clsAbc (instantiated when clsMainClass is created) Property DoSomeWork As clsDO_SOME_WORK Property DoSomeWork1 As clsDO_SOME_WORK1 Property DoSomeWork2 As clsDO_SOME_WORK2 Method Constructor DoSomeWork = New clsDO_SOME_WORK DoSomeWork = New clsDO_SOME_WORK1 DoSomeWork = New clsDO_SOME_WORK2 The question is: Inside of MainClassA -> "Property DoSomeWork" a reference must be obtained to ABC() However, since I want to keep all data "private" to the specific clsMainClass instance (A/B or C) how do I reference the correct one from within the class DoSomeWork? The only option I could think of, was naming three specific ABC()'s and doing it inside of the Module with either a protected or global scope. It would be 'cleaner' I think, if it was done as above but cannot figure out how to reference it properly and generically! Your ideas would be appreciated! Thank you, Tim Top doofus Post subject: Re: Class tree object coding?Posted: Sun Mar 31, 2013 10:00 pm Joined: Thu Sep 10, 2009 2:50 am Posts: 385 Location: Santa Cruz, CA, USA I think I follow. A module has 3 clsMainClass properties. Each clsMainClass has an ABC property and 3 worker properties. You want the workers to access ABC without exposing ABC from clsMainClass. The common point is clsMainClass, it has access to all the parts of interest. So it can keep ABC protected and pass a reference to the workers, like in their constructor. Workers just store the reference and use it when needed. Class clsMainClass ABC As clsAbc //protected (array?) DoSomeWork As clsDO_SOME_WORK //protected Sub Constructor() ABC = new clsABC DoSomeWork = New clsDO_SOME_WORK(ABC) //pass ref to worker End Sub End Class Class clsDO_SOME_WORK refABC As clsABC //protected Sub Constructor(passedABC As clsABC) refABC = passedABC //store for later access End Sub End Class You may also want to add to clsDO_SOME_WORK a protected default constructor (ie no parameters) if you want to prevent accidentally instantiating it without the ABC parameter. Or you could create a linkToABC method to pass it in instead of the constructor. Or you could pass in ABC as another parameter when calling worker methods that need it (if only called by clsMainClass). Also, is ABC supposed to be an array? If so does each worker need access to the array or just one element of? Top tseyfarth Post subject: Re: Class tree object coding?Posted: Sun Mar 31, 2013 10:26 pm Joined: Sat Dec 04, 2010 9:14 pm Posts: 876 Hi doofus, I just realized, that the web presented the code differently. It should have been: Property MainClassA As clsMainClass Property MainClassB As clsMainClass Property MainClassC As clsMainClass These are actually 3 different properties, an A, B & C. Not an array.... Quote:The common point is clsMainClass, it has access to all the parts of interest. That is correct - But in each class it creates (MainClassA, MainClassB, MainClassC) how do I reference *from within DoSomeWork, which was instantiated within MainClassA, a reference to MainClassA - such as MainClassA.ABC(0) - ie what is the correct name to substitute for MainClassA? Sorry for the ambiguity here! I am confused too, but know that to be generic, I cannot use MainClassA.ABC(0) nor MainClassB.ABC(0) etc.... If the property ABC was created in the Module, that would be easy - Module.ABC(0)...... See what I mean? Thanks again for helping! Tim Top tseyfarth Post subject: Re: Class tree object coding?Posted: Sun Mar 31, 2013 10:29 pm Joined: Sat Dec 04, 2010 9:14 pm Posts: 876 This might help to simplify - but also may be the wrong terminology.... How to reference a property that is owned by a mother class, from within a child class? Tim Top doofus Post subject: Re: Class tree object coding?Posted: Mon Apr 01, 2013 12:14 am Joined: Thu Sep 10, 2009 2:50 am Posts: 385 Location: Santa Cruz, CA, USA tseyfarth wrote:in each class it creates (MainClassA, MainClassB, MainClassC) how do I reference *from within DoSomeWork, which was instantiated within MainClassA, a reference to MainClassA - such as MainClassA.ABC(0) - That's what my code does, so I'm guessing you haven't used objects this way yet. Here's an explanation on that point. When clsMainClass sets up it's ABC property (which I understand to be an array) that property is protected. So you can't access that property from outside, but that property is really just a reference to an instance (not really true because it's an array but objects and arrays work the same in this respect). What you really want is for the workers to access the instance, which can be done by passing them a reference to it. The clsMainClass 'mother class' doesn't really 'own' the ABC property. Well, it owns the property but it doesn't own the instance. It can pass a reference to a worker and both access the same thing. In my code, the clsMainClass constructor instantiates a clsABC instance and stores it in it's ABC property. On the next line that reference is passed to a newly instantiated clsDO_SOME_WORK where the reference is stored in it's property refABC. At that point both clsMainClass.ABC and clsDO_SOME_WORK.refABC reference/point-to the same instance. I used the name refABC because that's my convention. Often there is a place I think of as the main storage/owner of an object but other places will get references to it. I prefix ref as a reminder that this isn't the main place the object is held. But that's all convention, there's isn't any owner, it's just a tangle of references. Scoping limits what is accessible using variable names but references can always be passed around. There's more to consider with your situation that taking it out of the abstract might help. I mean what clsMainClass does to manage the ABC array and how workers are supposed to work with it can change the design totally. For example, if clsMainClass changes around the instances of ABC then in the code I posted the workers will have old/wrong references. For this case maybe an event in the worker for retrieving the reference that clsMainClass implements with AddHandler would be better. Or possibly the simplest, if only clsMainClass is calling on the workers then it can pass in the clsABC reference when it calls a worker method. Then the worker just uses the clsABC it receives as a parameter. Top tseyfarth Post subject: Re: Class tree object coding?Posted: Mon Apr 01, 2013 12:31 am Joined: Sat Dec 04, 2010 9:14 pm Posts: 876 Wow! Lots to think about. It does make a lot of sense. I will take some time to review, study and try. But I *think* I see where you are going. If nothing more, you give me a leaping off point, which I am totally lacking up to now! Thanks again doofus- and I think you name should be changed Tim Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 6 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
