Creating objects
Posted
#1
(In Topic #582)
Trainee
as a hobbyist programming beginner, I am trying to get into object-oriented programming. The aim being to be able to create programs with GUIs for Linux (in the long run).
Recently I discovered Gambas and have been playing around with objects and trying to learn their concepts and how they are created.
As I have come to understand, there seem to be different ways of how an object may be created in Gambas.
My questions are:
- When and why would I use Version 1 over the others?
- What are the implications of each version?
- Have I missed anything?
Inspired by SteveDee's great "A Dirty Guide to OOP #1" https://forum.gambas.o…e/viewtopic.php?f=4&t=816, I decided to also go with fruits for my examples :-)
The Fruit-Class:
The Main-Module:
Version 1
Code (gambas)
- ' Gambas module file
- 'init the fruit
- oApple.name = "Apple1"
- oApple.colour = "Green"
- 'show what's happened
Version 2
Code (gambas)
Version 3 a)
Code (gambas)
- 'Gambas module file
- 'using a variable for the class name; the >>As "Apple"<< bit is the event-handler
- 'init the fruit
- oApple.name = "Apple3"
- oApple.colour = "Yellow"
- 'show what's happened
Version 3 b)
Code (gambas)
- 'Gambas module file
- 'init the fruit
- oApple.name = "Apple4"
- oApple.colour = "Brown"
- 'show what's happened
Thanks for your help!
Seb.
Posted
Enthusiast

1: Public oApple As Fruit -> Lives in the whole application -> [ModuleName. || ClassName.]oApple
Instantiated with NEW
1: Private oApple As Fruit -> Lives in the whole Module / Class
Instantiated with NEW
2: Dim oApple As Fruit -> Lives in the Procedure/Function where it's Dim'ed
Instantiated with NEW
Posted
Guru

In version 1 oApple can be accesses from any other function because it's scope is a global variable.
In version 2 oApple is defined in Main() and only visible to Main() unless passed as an argument.
much info is here…
/cat/object - Gambas Documentation
If you are a beginner I'd suggest getitng used to "using" existing objects and how it all works before figuring out how to make them.
It ranges from fairly simple the pretty complicated so one step at a time innit
Other folks here will be able to point you to already written walkthroughs on how to best create and use objects/classes in oop
Bruce
Posted
Regular

I've just notice that PJ has replied since I started composing this answer, so there may be some duplication.
Version 1 & 2 are a bit pointless because you are creating an instance of the object as soon as the programs starts, so you may as well just Declare and create an Instance at the top of the class like this:-
If you add a declaration like:-
…then you delay the creation of the object until if/when the routine is called. However, this object is not destroyed when you leave myRarelyCalledRoutine()
In VB I would have delayed creating an instance of an object until I needed it, and then I would have destroyed it to save memory (RAM) with something like; Set oApple = Nothing
But, I don't think you can destroy objects with a command in Gambas
However, if you Declare AND create an Instance of an object in a routine like this:-
…the object will be destroyed when you leave myRarelyCalledRoutine()
As for Events, if your object needs to raise an event like "myFruitHasDroppedFromTheTree" then you will need something like your examples.
I hope that helps…a bit!
Posted
Regular

BruceSteers said
…In version 1 oApple can be accesses from any other function because it's scope is a global variable…
Is it "global" Bruce?
I'd have to double check this, but I think its only Public to the class that it is declared within. So not directly accessible from (say) other forms, except via dot notation; FormA.PublicVariable
Posted
Enthusiast

stevedee said
I've just notice that PJ has replied since I started composing this answer
sorry steve …
Posted
Enthusiast

PJBlack said
-> [ModuleName. || ClassName.]oApple
…
stevedee said
I'd have to double check this, but I think its only Public to the class that it is declared within. So not directly accessible from (say) other forms, except via dot notation; FormA.PublicVariable
Posted
Trainee
What I gather is, that
- I need to learn about the scope of variables and objects
- I need to undersand the difference between the declaration and the instanciation of objects, and then where/when best to do what (or when/where to combine them)
- I need to learn about destructing (destroying?) objects in order to prevent me running out of memory (Ahhhh, the penny dropped: THIS is what Java does automagically and calls "garbage collecting", right?!)
Thanks!
Greetings, Seb.
Posted
Enthusiast

Seb said
a) I need to learn about the scope of variables and objects
thats easy :shock: … the scope begins at the place where you create downwards the ancestry (inheritance)
Seb said
b) where/when best to do what (or when/where to combine them)
public blahblubb as classname -> just the plan for building a house (no house build yet)
blahblubb = new classname[(parent)] -> now the house is build and ready to use
Seb said
c) THIS is what Java does automagically and calls "garbage collecting", right?!)
normally there is no need to destroy your objects cause gambas will do that automatically … and yes … that's the garbage collection
1 guest and 0 members have just viewed this.


