[Gambas-user] File opening dialog and one more question

Dmitrij Malkov dima.malkov.russia at ...626...
Fri Jan 6 10:45:52 CET 2012


"Static" means:

- For a variable, that the variable is stored in the class, not in any
object. So there is only one such variable. Its value does not depend on
the object.

- For a method (or a property), that the method cannot use any
non-static symbol (ME is NULL).

- For a constant, nothing, as a constant is always static (as its value
cannot change).

I hope it will be clear for you!

Regards,

-- 
Benoît Minisini


I am interesting in STATIC too.
I think a real example will be a better explanation.

Create new console project and put the next text in MMain.module

|' Gambas module file

Public Sub Main()

   'Use <<AS NEW CUnit>> instead <<AS CUnit>>
   'to activate constructor
   Dim Unit1, Unit2 As New CUnit
   Dim Unit3, Unit4 As New CUnit

   'Theese objects are not created yet?
   Dim Unit5, Unit6 As CUnit

   'We have 4 objects created
   CUnit.HowManyUnits()

   'Uncomment last two lines and see
   '"Unexpected DIM as MMain.module:20"
   'In C++ that will not cause an error

   'Dim Unit7 As CUnit
   'CUnit.HowManyUnits()
End|


And create new class "CUnit" with text

|' Gambas class file

Static Private NumberOfUnits As Integer = 0

'constructor
Public Sub _new()
   NumberOfUnits += 1
   Print "New Unit was created"
End

Static Public Sub HowManyUnits()
   Print "There are "; NumberOfUnits; " objects of class 'CUnit'"
End|

Output in console:
|New Unit was created
New Unit was created
New Unit was created
New Unit was created
There are 4 objects of class 'CUnit'|

Now comment all "DIM ..." lines and run.
Output in console without any error:
|There are 0 objects of class 'CUnit'|

This code is not clear to me. What the difference between "PUBLIC SUB 
_new()" and "PUBLIC SUB New()"
? And why I can't create objects of class CUnits after calling 
CUnit.HowManyUnits()?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OOP_3_STATIC-0.0.1.tar.gz
Type: application/x-gzip
Size: 4965 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20120106/819f008f/attachment.bin>


More information about the User mailing list