what about the classes?

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
vito49
Posts: 8
Joined: Sunday 20th December 2020 8:19am

what about the classes?

Post by vito49 »

Hello guys, I wonder why there are "classes" to use ... but I have no clue why they are used and how to use them.
Sorry, I'm beginner (not in time, but in apps I already composed)
And I'm not English, so please bare that in mind .... :) :)
Thanks
Vito
born 19 may 1949
using Gambas3 for therapy ;)
on a Manjaro XFCE4 PC
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: what about the classes?

Post by stevedee »

vito49 wrote: Tuesday 2nd February 2021 10:25am Hello guys, I wonder why there are "classes" to use ... but I have no clue why they are used and how to use them...
In Object Orientated Programming, a class is a kind of template that you can use to create a new "instance" of an "object".
Sounds scary?

When you simply draw a control Button on a form, you use the Button class code to create a new instance (new version/new Button object) of the software that manages a button. This new button object allows you to customise your button with a new name, a new text caption, new size, new colour...and so on.

If we did not have a Button class we all would have to write our own code to draw our own buttons and control how they work ...we would all be re-inventing the wheel!

I'll try to come up with a simple class example with its own Methods (Functions) and Properties.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: what about the classes?

Post by stevedee »

Actually, just returning to the Button class may be the best example.

Start a new GUI Gambas project, but don't draw any components on the Form, just add this code to FMain.class:-
' Gambas class file

Public myButton As Button

Public Sub Form_Open()
  'create a new instance of the Button Class
  'and draw it on the main form (its Parent)
  myButton = New Button(FMain)
  myButton.Height = 100
  myButton.Width = 200
  myButton.top = 50
  myButton.Left = 100
  myButton.Text = "OK"
  
End
What this code is doing is creating a new button from the existing code class called Button. We call this creating a new instance of the class Button by declaring myButton as a Button.

We can then use the Button class Properties to set size, colour, text & so on.

Is this helping?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: what about the classes?

Post by cogier »

I have been working on this as well. Here is an example using a class to create a list of your music collection.

The Class may seem a little complicated, but I hope you can see how this small amount of code can do so much.
Public Sub Form_Open()

  Dim NewAlbum As Record

  NewAlbum = New Record(Me, "Sound of Silence", "Simon & Garfunkel", "SoundOfSilence.jpeg", "60s Pop")
  NewAlbum = New Record(Me, "Paranoid", "Black Sabbath", "Paranoid.jpeg", "Heavy Metal")
  NewAlbum = New Record(Me, "Carpenters", "Carpenters", "Carpenters.jpeg", "Pop")

End
Image
ClassExample-0.0.1.tar.gz
(38.34 KiB) Downloaded 289 times
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: what about the classes?

Post by PJBlack »

the carpenters ??? really ??? ;)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: what about the classes?

Post by cogier »

PJBlack wrote: Tuesday 2nd February 2021 2:17pm the carpenters ??? really ??? ;)
Really!! :D :D
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: what about the classes?

Post by stevedee »

cogier wrote: Tuesday 2nd February 2021 2:38pm
PJBlack wrote: Tuesday 2nd February 2021 2:17pm the carpenters ??? really ??? ;)
Really!! :D :D
...he's only just begun
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: what about the classes?

Post by PJBlack »

yeah ... party :D

back to topic:
charlie ... i'm an f***ing old procedural programmer with big fears of OOP for a long time and it tokks me a lot of hard work to get into it ... but while understanding how it works or maybe how i should work i will say "oh wow ... how could i program without that" ... taking your second example a newbie would likely say "oh hell ..." but yes it shows what a class can be

vito:
fight your way trough ... programing is fun and you will find solutions for problems that you won't have without programing ;)

you may like to have a look here:

https://gambas-buch.de/dwen/doku.php?id=start

for years now hans lehmann wrote on that and for a starter it is very helpfully, most of it should be in english but also avail in german ... i you have any questions the carpenters and the rest here will try to help you :)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: what about the classes?

Post by cogier »

...he's only just begun
I'm beginning to feel unwell :oops:
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: what about the classes?

Post by stevedee »

Further reading from 12 months ago: https://forum.gambas.one/viewtopic.php?f=4&t=816
Post Reply