Creating objects

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Seb
Posts: 2
Joined: Monday 15th February 2021 9:45am

Creating objects

Post by Seb »

A friendly "hello" to all fellow Gambas-users!

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.one/viewtopic.php?f=4&t=816, I decided to also go with fruits for my examples :-)

The Fruit-Class:
' Gambas class file

Property Name As String Use sName          'shorthand syntax for declaring the properties of a class,
Property Colour As String Use sColour      'setter and getter methods are created "under the hood", using the given variables.

The Main-Module:
Version 1
' Gambas module file

Public oApple As Fruit          'declaration of the object-variable outside of Sub Main()

Public Sub Main()

  oApple = New Fruit             'creation of the object inside Sub Main()

  'init the fruit
  oApple.name = "Apple1"
  oApple.colour = "Green"
  'show what's happened
  Print "Name: " & oApple.Name
  Print "Colour: " & oApple.Colour
End
Version 2
' Gambas module file

Public Sub Main()

  Dim oApple As New Fruit          'declaration of the object-variable and creation of the object inside of Sub Main()

  'init the fruit
  oApple.name = "Apple2"
  oApple.colour = "Red"
  'show what's happened
  Print "Name: " & oApple.Name
  Print "Colour: " & oApple.Colour
End
Version 3 a)
'Gambas module file

Public Sub Main()
  Dim sClassName As String = "Fruit"

  Dim oApple As Object = New (sClassName) As "Apple"          'declaration of the object-variable and creation of the object, 
                                                              '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
  Print "Name: " & oApple.Name
  Print "Colour: " & oApple.Colour
End
Version 3 b)
'Gambas module file

Public Sub Main()
  Dim sClassName As String = "Fruit"
  Dim sObjectName As String = "Apple" 
 
  Dim oApple As Object = New (sClassName) As (sObjectName)        'using a variable also for the event-handler

  'init the fruit
  oApple.name = "Apple4"
  oApple.colour = "Brown"
  'show what's happened
  Print "Name: " & oApple.Name
  Print "Colour: " & oApple.Colour
End
Thanks for your help!
Seb.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Creating objects

Post by PJBlack »

afaik ...

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
Last edited by PJBlack on Monday 15th February 2021 2:49pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating objects

Post by BruceSteers »

Mostly the difference is the scope of the variable oApple

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...
http://gambaswiki.org/wiki/cat/object

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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Creating objects

Post by stevedee »

Hi Seb, welcome to GambasONE.

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:-
'Gambas module file

Public oApple As New Fruit


If you add a declaration like:-
' Gambas module file
 
Public oApple As Fruit          'declaration of the object-variable outside of Sub Main()
 
Public Sub myRarelyCalledRoutine()
  oApple = New Fruit 
 
...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:-
Public Sub myRarelyCalledRoutine()
 
  Dim oApple As New Fruit          'declaration of the object-variable and creation of the object inside of Sub Main()
 
  'init the fruit
  oApple.name = "Apple2"


...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!
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Creating objects

Post by stevedee »

BruceSteers wrote: Monday 15th February 2021 2:49pm ...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
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Creating objects

Post by PJBlack »

stevedee wrote: Monday 15th February 2021 3:12pmI've just notice that PJ has replied since I started composing this answer


sorry steve ... ;)
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Creating objects

Post by PJBlack »

PJBlack wrote: Monday 15th February 2021 2:46pm-> [ModuleName. || ClassName.]oApple
...
stevedee wrote: Monday 15th February 2021 3:19pm 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
Private oApple As Fruit ' global for the mod/class
Public oApple As Fruit ' global for the mod/class AND visible/addressable for/from the whole application
 
Seb
Posts: 2
Joined: Monday 15th February 2021 9:45am

Re: Creating objects

Post by Seb »

Wow, thank you all for your helpful replies!
What I gather is, that
a) I need to learn about the scope of variables and objects
b) 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)
c) 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?!)
This'll give me enough to think about for a while, I expect :)
Thanks!
Greetings, Seb.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Creating objects

Post by PJBlack »

Seb wrote: Monday 15th February 2021 4:46pm 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 wrote: Monday 15th February 2021 4:46pm 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 wrote: Monday 15th February 2021 4:46pm 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
Post Reply