[Gambas-user] dynamic created Menus

Gianluigi bagonergi at ...626...
Thu Feb 16 11:34:51 CET 2017


Hi Tobias,
first of all I apologize to you for the late reply.
You really been kind and patient to answer in a broad and comprehensive
manner to a blockhead like me.
I confirm that you are a great and thank you very much.

Regards
Gianluigi

2017-02-16 0:45 GMT+01:00 Tobias Boege <taboege at ...626...>:

> On Wed, 15 Feb 2017, Gianluigi wrote:
> > Hi Tobias,
> >
> > kindly can you deepen the concept of static.
> > See attached project.
> >
>
> For reference: this is the Main routine
>
>   Public Sub Main()
>     Dim cl As New Class1
>
>     Print cl.myAdd(12, 12)
>     Print cl.myAdd()                  ' I expected zero
>     Print Module1.myAdd(12, 12)
>     Print Module1.myAdd()             ' I expected 24
>   End
>
> and in Class1 as well as Module1 we have
>
>   Public Function myAdd(Optional a As Integer, b As Integer) As Integer
>     Return (a + b)
>   End
>
> where one is a static method and the other is not.
>
> Let me first explain static vs. dynamic in general, then you'll see why the
> code prints
>
>   24
>   0
>   24
>   0
>
> You can declare variables, properties and methods as either static or
> dynamic in Gambas. If you don't declare it as static, then it's dynamic.
> If a variable is dynamic, then each object you create from the class
> receives its own separate memory region for the variable, so the value
> of the variable can be different in every object. This is usually what
> you want (hence there is no extra keyword to make things dynamic, they
> are by default, unless you make a module). If you declare a variable as
> static, you can think of the variable belonging not to an object but to
> the class itself. All objects you create from the class will share the
> same memory region for a static variable. If you modify the variable
> from one object, the change is visible in all other objects.
>
> This is what happens to variables. If you make a method static, then it
> also "belongs to the class" (not to dynamic objects), in the sense that
> you can only access static variables from a static method. Lastly a static
> property is just implemented by using the two static Property_Read() and
> Property_Write() methods, so the explanation of static methods applies
> here as well.
>
> Now about your code: the myAdd() method just calculates the sum of its
> arguments. It does not access any variables. Whether something is static
> or not only makes a difference if you access memory in your class or
> object.
>
> The attached project serves better to highlight the difference, because
> it actually *stores* values, once statically and once dynamically:
>
>   ' Main.module
>   Public Sub Main()
>     Dim x, y As New Class1
>     Dim u, v As New Module1
>
>     x.Add(10)
>     y.Add(5)
>     x.Print()
>     y.Print()
>     Print "---"
>
>     u.Add(10)
>     v.Add(5)
>     u.Print()
>     v.Print()
>   End
>
>   ' Class1.class and Module1.module identical code
>   Public sum As Integer
>
>   Public Sub Add(a As Integer)
>     sum += a
>   End
>
>   Public Sub Print()
>     Print sum
>   End
>
> Output is
>
>   10
>   5
>   ---
>   15
>   15
>
> because in the first half, x and y have a dynamic sum variable, i.e. both
> objects have their own variable, whereas in the second case sum is static,
> so both additions actually go to the same region in memory and you get
> 10+5 = 15.
>
> Regards,
> Tobi
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>



More information about the User mailing list