Know from the class which instance it is

I have a Container containing methods. This Container control may have several instances, on several windows or other container controls.

Is it possible from the class to know
1 - Which instance is calling the method
2 - The properties of the instance (left, top, width, etc.)
3 - The properties of the instance self (window.left, top, etc.)

I figure I can add properties to the class set from the instance events or the window, but I may have overlooked other possibilities.

Thank you in advance for your insight.

I would yes to all 3. I am not really sure if I understand what you are doing. I may just be stating the obvious here but here is my attempt at an answer:

  1. Refer to the instance itself as ‘me’
    The properties and implementations of events is what makes each instance unique (on a window)
    2)‘me.left’, ‘me.top’, etc.
  2. You could refer to it with ‘me.window’. If you are wanting to access properties specific to that implementation of a containercontrol/window then use read this:

For casting - You might want to implement this with different techniques
You could write a method to refer to the window or you could perform what the method does directly (without a method). I suppose it depends on how many times you want to reference the window. That is if you want to cast the window.
You do not need to cast the window/containercontrol containing it to access properties like ‘left’, ‘top’ but you cast for properties specific to the superclass of that instance.
Cast with something like ‘YourContainerControlOrWindowSubclass(me.window)’

Hope this helps

What about using an Interface and providing it as a parameter in your method(s)? The Interface can implement methods to access the properties you need.

I do not understand why he would use an interface if it instances rather than subclasses?

Not saying your wrong. Just a bit unsure whether I understood the OP.

Thanks

I’m envisioning that the Windows and ContainerControls that might call these methods are assigned the Interface. Let’s call it CallerInterface.

The methods could be implemented as Sub MyMethod (fromInstance As CallerInterface, otherParams...), and called like MyMethod( me, otherParams ).

[quote=140481:@Michel Bujardet]
Is it possible from the class to know
1 - Which instance is calling the method
2 - The properties of the instance (left, top, width, etc.)
3 - The properties of the instance self (window.left, top, etc.)[/quote]

1 - Probably but I would avoid that
Having a means to ask the caller for information (via events or passing in a reference to it - using an interface or not) is probably better
Depending on how you address #1 #2 and 3 are certainly possible

Using events to get data back from the places where you have embedded the control is really useful

[quote=140561:@Norman Palardy]1 - Probably but I would avoid that
Having a means to ask the caller for information (via events or passing in a reference to it - using an interface or not) is probably better
Depending on how you address #1 #2 and 3 are certainly possible

Using events to get data back from the places where you have embedded the control is really useful[/quote]

OK. So I now have to learn about Interfaces, which I did not do until now. Thank you all.

Depending on what it is you need to know from the window or whatever contains this container events might be all you need

For instance if you have a Window that has an instance of this container & the container needs to refer to … a button on the window

On the container you add an event definition - WhatButton() as PushButton
In your code where you need to know this button you can do

dim theButtonToEnable as PushButton = raiseEvent WhatButton() if theButtonToEnable <> nil then /// do whatever end if

then you can add one by drag & drop and implement the event OR in code & add handler

and as long as your code that needs this information is written to handle NOT getting a response you’re good to go

[quote=140601:@Norman Palardy]Depending on what it is you need to know from the window or whatever contains this container events might be all you need

[/quote]

I am sorry, I do not see how using that method I would be able to get the Left property of, say, a Button1 placed on the same window as my instance from the class.

Well you’d get back a reference to the button
check its left property

dim theButtonToEnable as PushButton = raiseEvent WhatButton() if theButtonToEnable <> nil then // do whatever if me.left = thebuttonToEnable.Left then msgbox "we're aligned!" thebuttonToEnable.Enabled = false else msgbox "we're NOT aligned!" thebuttonToEnable.Enabled = true end if end if

Basically the EVENT asks the implementer of the event to give it back some value (in this case the reference to the button) rather than rooting around in the window guts for itself

It depends how you want to implement it.
In general, if you had a button on different windows. You would use an event to reference that button and return a reference of that button for each window.

If you wanted to directly refer to specific instances cast the window.

You could use this:

 dim theButtonToEnable as PushButton = raiseEvent WhatButton()
    if theButtonToEnable <> nil then
         /// do whatever
WhatButton.Left = 12
    end if

Or this;

WhatButton.Left = 12

Personally, I have never used ‘raiseEvent’ before. You can just write the name of the event definition without that. It would depend on your preference. It could make it easier to tell when you are referring to an event and when you are referring to a method.

[quote=140609:@Norman Palardy]Well you’d get back a reference to the button
check its left property

dim theButtonToEnable as PushButton = raiseEvent WhatButton() if theButtonToEnable <> nil then // do whatever if me.left = thebuttonToEnable.Left then msgbox "we're aligned!" thebuttonToEnable.Enabled = false else msgbox "we're NOT aligned!" thebuttonToEnable.Enabled = true end if end if

Basically the EVENT asks the implementer of the event to give it back some value (in this case the reference to the button) rather than rooting around in the window guts for itself[/quote]

I am not too clever tonight. I had not understood this code is in a method of the class. What I do not get yet is what is what. How do I reference the particular button on the window ? Should I go

dim Button1 as PushButton = raiseEvent WhatButton()

OK try this

  • new desktop project

  • add a new class and make it a subclass of PushButton - call it “customPB”

  • to this new class add an event definition - “TheOtherPB() as PushButton”

  • to this new class add an event definition - “Action()”

  • add the EVENT Action (we’re going to implement it in our subclass and pass it along)

  • Put this code in it

    [code]
    dim otherPB as pushbutton = raiseevent TheOtherButton()

    if otherPB <> nil then
    // we’re going to toggle the otherbuttons enabled state when you push this one
    otherPB.Enabled = not otherPB.Enabled
    end if

    Raiseevent Action
    [/code]

  • add a push button from the library to the default window (as long as it’s a pushbutton it won’t matter)

  • name this “PushButton1”

  • drag an instance of your new subclass, customPB, onto Window1

  • implement its TheOtherButton event with
    return PushButton1

IF you’ve done all this correctly then whenever you press the customPB instance the other button will be disabled
AND neither button really has any code that specifically refers to the other by name

in your window you implement the WhatButton event like so:

Function WhatButton() as PushButton
   return PushButton1
End

That gives your class a reference to the button on the window without having to know anything about the window itself.
Sub MethodInTheClass
dim Button1 as PushButton = WhatButton // raiseEvent is optional
dim X as integer = Button1.Left
// etc
End
[/code]

[quote=140615:@Michel Bujardet]I am not too clever tonight. I had not understood this code is in a method of the class. What I do not get yet is what is what. How do I reference the particular button on the window ? Should I go

dim Button1 as PushButton = raiseEvent WhatButton()

In the subclass/instance (presumably of a window or container control) you implement the WhatButton event to return the desired pushbutton. Then in the superclass you can access the pushbutton through Button1 and use it as you would the name of the button if the code was on the window directly (i.e. Window1.Pushbutton1 is replaced with Button1, and the code is no longer dependent on the implementation of the window).

Thank you for the detailed instructions. Now I think I understand how to proceed :slight_smile:

[quote=140619:@Tim Hare]in your window you implement the WhatButton event like so:

Function WhatButton() as PushButton
   return PushButton1
End

That gives your class a reference to the button on the window without having to know anything about the window itself.
Sub MethodInTheClass
dim Button1 as PushButton = WhatButton // raiseEvent is optional
dim X as integer = Button1.Left
// etc
End
[/code][/quote]
good catch
writing the example & instructions at the same time & missed a whole step
bad norm

This works purrfectly :slight_smile:

Thank you Norman. I learned something today :slight_smile: