Shared method

From REALbasicWiki

Jump to: navigation, search
Overall article skill Skill ranges from beginner (green) to expert (red)

A shared method, more appropriately called class method in some other languages, is a method attached to a class rather than to an instance of the class.

It is invoked as class_name.method_name

[edit] Implementing constructors as shared methods

The major use of shared methods is to implement constructor methods. Consider the following cases:

  1. When creating an instance of a class, the instance is created before the constructor is called. If there is a error in the constructor and the instance eventually should not be created... well it is too late and you will end up with a non-functional instance, but you may not know that it is non-functional. Would not it be better if the function actually return a nil object instead ?
  2. You may want to have 2 constructor methods which both take a single string as a parameter. In such a case, the compiler will complain that it cannot choose between the two methods because they have the same parameter types. So you would have to work around that.

In such cases, you may want to use shared methods.

  • If the instantiation of the object may fail, you can make a shared method which will return nil if an error occur, instead of a non-usable instance. Your shared method will take any parameters you need and return an instance of the class. So if your class name is "myGreatClass", then your shared method will return a "myGreatClass" instance.
Shared function NewObject(param1 as string) as myGreatClass
dim resultInstance as myGreatClass //This is the instance you will return if no error occurred
//Do whatever you need here

if error_occurred then
return nil
else //No error
return resultInstance
end if
End function

Now if instantiation failed, you will get a nil object; if it works, you will get a myGreatClass instance and you are sure that it is going to work (unless bugs exists elsewhere).

dim myInstance as myGreatClass

myInstance = myGreatClass.NewObject( ..parameters.. )
if myInstance = nil then
MsgBox "Could not instantiate myGreatClass"
else
//.... you can safely use myInstance
end if
  • Imagine that your myGreatClass can be instantiated using different kinds of string, say XML data and binary data. You cannot make two constructor methods accepting a single string because the compiler will not be able to choose which one is to be used.
Shared function NewFromXMLData( data as string ) as myGreatClass
//Do what you need with the XML data and return a myGreatClass instance
End function

Shared function NewFromBinaryData( data as string ) as myGreatClass
//Do what you need with the XML data and return a myGreatClass instance
End function

//Now if you want to instantiate myGreatClass from two different string types
dim instance1, instance2 as myGreatClass
dim myXMLdata, myBinaryData as string
//... set the values of the two strings

instance1 = myGreatClass.NewFromXMLData( myXMLdata )
instance2 = myGreatClass.NewFromBinaryData( myBinaryData )

Personal tools
related