Shared Methods: Protected

What little I can find in the documentation implies that shared methods are similar to a module method.

There do not seem to be many ways that it acts like a module method.

Module methods can be Global, Protected, and Private

Shared methods can be Public, Protected, and Private

You can declare a shared method as being Public and then I can use it “outside” of the class by calling it with NameClass.theMethod. In this sense, it is like a Protected module method which can be called with NameModule.theMethod. Public is not analogous to Global. You cannot call a shared method that is Public without the prefix of the name of the class. You could call a Global module method without a prefix.

If you declare a shared method as being Protected, then it seems to be like a module method that is Private. It can only be used within the class itself . So a Protected shared method seems analogous to a Private module method.

So this brings up the question to me: What is a Private shared method? How is it different than a Protected shared method?

Why does Xojo give you the option of declaring a shared method as Public or Protected or Private? It seems to me that Protected is identical to Private when considering a shared method.

Thanks for any help clarifying this.

Unless you have an instance of a class that is active, you cannot call any of the methods of that class.

Methods within a module can be called al the time.

So for example, in a business application, you cannot call the methods of class InvoiceDoc unless you instanciated that class and you have a Invoice (as new InvoiceDoc) somewhere in your session. If you have a method called ReadPDFInvoice somewhere in a module, you can call that method at any given time. No class instance required.

But in my experience, you can call a shared method of a class if that shared method is Public even if no instance of that class has ever been created in your project.

WOW… you can… To me that should not be allowed…

Shared methods are part of the language design. Most people don’t need them in most cases, but they’re handy if you want to do something like enforce a single instance (rather than use a constructor). You can see the Design Patterns Singleton example included with Xojo, or the docs here: http://developer.xojo.com/userguide/classes$Shared%20Methods

In my grossly stripped down sample project I have a class called

pt2D.

It contains a shared method called blackAdder.

blackAdder takes two parameters (point1 As Double, point2 As Double) and returns a Double.

blackAdder is Public

The code for backAdder is
Return point1 + point2

In my main window I have a PushButton1 whose Action is

Dim ant As Double
ant = pt2D.blackAdder(5.1, 6.2)
MsgBox("BLACK: " + Str(ant))

It works fine. No instance of pt2D has ever been created.

Travis I think you missed the paradox. You DON’T HAVE TO CREATE AN INSTANCE to call “SHARED” methods. NON-Shared methods are not accessible. But a “Shared” method acts like the “CLASS” is a “MODULE” almost

to me it makes sense if you have TWO instances of a class, and they SHARE the method… but to me, there should be at least ONE instance for the class method to be visible

Well, Robert, I just learned something new. Now, why would I create methods in a class, that can operate independently from the class? My instinct says they should go in a module. I’ll have to think about this…

[quote=275878:@Dave S]Travis I think you missed the paradox. You DON’T HAVE TO CREATE AN INSTANCE to call “SHARED” methods. NON-Shared methods are not accessible. But a “Shared” method acts like the “CLASS” is a “MODULE” almost
to me it makes sense if you have TWO instances of a class, and they SHARE the method… but to me, there should be at least ONE instance for the class method to be visible[/quote]

That’s the point. A shared method doesn’t require an instance of the class. That’s how you can use them to return a single instance of a class (i.e., the class manages its own, single, instance in the background in the singleton pattern) rather than a constructor. They wouldn’t be able to perform that function of returning a single managed instance if they required an instance of the class to be called :slight_smile:

Then why bother… park the code in a method and don’t confuse people…

A “Shared Method” in a class, should ONLY be able to be shared between existing Instances of that class… NOT the public (expect via an existing instance)

Thats my opinon… doesn’t matter to me as I don’t use them anyways…

…oops. Just deleted my post. Looked like a double post…

So the original question:

A private shared method can be accessed within the class

A Protected Shared method can be accessed within the class and any subclass of the class.

Every language typically has some concept of instance methods vs. class methods. In Xojo instance methods are typically just called “methods” and class methods are called “shared methods”.

Encapsulation and namespacing.

A shared method marked Public is equivalent to a module method marked Protected.

A shared method marked Protected can be used by the code in the class and any subclass.

A shared method marked Private can be used by the code in the class definition only. Subclasses cannot call it.

There are reasons for all of the above. If a module method fits your needs, use it. One is not necessarily better than the other. It all depends on the design of your app.

Louis Desjardins asks:

My intent in real life was to organize my code in a simple fashion. I had a class pt2D (point in 2D space) that had two properties (x and y) that specified the location in space.

I needed a function that returned the distance between the two points. Now I could put that function in a module that took two parameters (two instances of the pt2D class) and that would work fine. But I thought, why not just make it a shared method so it was all “together” in the organization of the project.

So I created a shared method (calcDistance) of pt2D that would take two parameters (two instances of the pt2D class) and return the distance between them.

If I made that shared method Public then I could call it “outside” of the class itself. My code dealing with the points in 2D space was therefore all “in” the class organizationally. I could have written the distance function in some module. That works, but requires creating the module which seems unnecessary.

But this exercise required that the shared method be Public. And you have to call it with the construction: pt2D.calcDistance. A Global module method would not require a prefix (name of the module) but a Protected module method would.

If the shared method is Protected or Private, then it will not work in my context. But I wondered why, when Protected, pt2D.calcDistance was not allowed to be used outside the class itself. If it were a module method it would be useable outside the module.

And finally, trying to understand the entire topic, I wondered what is the difference between Protected and Private when considering a shared method.

McKay basically answered my question and Hare has cleanly summarized the whole thing in a way that I find clear.

One of his statements in particular speaks to me.

[quote=275900:@Tim Hare]A shared method marked Public is equivalent to a module method marked Protected.
[/quote]
EXCEPT that in a shared class method you CAN create an instance & access the properties of the instance regardless of whether they are private public etc since the SHARED method IS part of the class so it can access all the innards

A Module method cannot do this