Scope

[quote=175521:@Dave S]PERFECT! works just the way I wanted…
and I learned something new… proving you CAN still teach an old dog new tricks (even if he does growl while doing so)[/quote]

I have used that design patterns a number of times… but it seems like a lot more work to set up than it should be.

RB had interfaces from when I first came in (RB3). While you could not do the friend thing back then, I found them very useful from the start!

  • Karen

+1 on interfaces solving a bunch of problems.

Well that solved one problem, and now I have created the opposite problem
Same classes as above “A” has a collection of Class “B”
But now if any instance of Class “B” has a property value changed, I need to notify Class “A”

I tried having Class “B” raise an Event, but since the event was in an “instance”, I could determine no way for “A” to see that event.

So I tried to create another Interface :slight_smile:

CLASS “B”

SUB shared_code(assigns new_value as string)
Class_A_Interface(shared_code)=new_value  // This line generates below ERROR
END SUB

Class “A”

SUB shared_code(assigns new_value as string)
.... do something with new_value
END SUB

Class_A_Interface [implemented by CLASS_A]

SUB shared_code(assigns new_value as string)

ERROR Message in CLASS_B

Assignment accessor must be invoked by assigning a value
and it highlights “shared_code”

I don’t see any part of this that ISN’T assigning a value

This line doesn’t make any sense

Class_A_Interface(shared_code)=new_value

What are you trying to do here? Minimally, when you cast it should look like

Class_A_Interface(shared_code).xyz=new_value

But in any case, I would use AddHandler to catch the events from B.

sorry… “XYZ”? where’d that come from?

“Shared_Code” is the name of the method in both Classes
When it is invoked in an instance of CLASS_B, I need that action to invoke the same in CLASS_A

So I guess the line should be

CLASS_A_INTERFACE(xxxxxx).shared_code=new_value

But then what is xxxxxx supposed to be… I would have thought it would be “class_a” but that gives me

This is a type name, not a variable, values can't be assigned to it
Class_A_Interface(class_a).shared_code=new_value

previous post has all the involved code in it

I started trying to use Addhandler… but class_A needed a specific instance, and there aren’t any until the application is underway

I’d love to see an example

[quote=175915:@Dave S]Well that solved one problem, and now I have created the opposite problem
Same classes as above “A” has a collection of Class “B”
But now if any instance of Class “B” has a property value changed, I need to notify Class “A”[/quote]

Why won’t AddHandler work? Assign the Handler from classA To the instance of ClassB when you add the ClassB to the collection.

Alternatively Add a reference to ClassA itself (or a weak reference to it) to each classB instance when added to the handler and use another private Interface to A that allows ClassB to call a Method on ClassA

  • Karen

so if there are 100 instances of Class_B then I need 100 AddHandlers?

Actually adding a “parent” reference to each instance of Class_B might be the best overall

What about using the observer pattern?

[quote=175926:@Dave S]so if there are 100 instances of Class_B then I need 100 AddHandlers?
[/quote]

What I do in that case with delegates is not use addhandler. I create 1 delegate instance to the method that I store in the parent Class A (usually created in the constructor). Then when I assign an instance of B to A, I have the instance store a reference to that delegate in a property.

That way I have 100 references to a single delegate instead of 100 independent delegates to the same method on the same instance of Class A

I find that simpler than using AddHandler as I can just Nil the reference in B (or just assign a reference to a delegate from a different parent instance. Besides saving overhead, doing it that way is a bit simpler and more flexible than having to fiddle with RemoveHandler.

That is what I was suggesting alternately if I was not clear… Though Maybe a weak reference would be best… and as I said you can use another private interface to hide the method call from the outside.

[quote=175926:@Dave S]so if there are 100 instances of Class_B then I need 100 AddHandlers?
[/quote]
Oh god …
AddHandler is great for some things but its not a substitute for a well designed API