Creating a class requirig mandatory parameters

Post

Posted
Rating:
#1 (In Topic #1532)
Regular
DIYTinkerer is in the usergroup ‘Regular’
Hi,
I'm trying to create a new class something that requires 3 parameters i.e.

Code (gambas)

  1. Public centerX As Integer = 400
  2. Public centerY As Integer = 400
  3. Public radius As Float = 400
  4. Public corrector As New OPC(centerX, centerY, radius)

in a separate class file in the project I've defined the class as follows…

Code (gambas)

  1. Public OPC(centerX As Integer, centerY As Integer, radius As Float) As Class

but get the error…
Missing AS (OPC.class:3)

What am I doing wrong?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’

DIYTinkerer said

but get the error…
Missing AS (OPC.class:3)

What am I doing wrong?
You are trying to create a new method but a class is required :?

If i understand what you try to achieve:
You need to make a class named OPC. Then in OPC:

Code (gambas)

  1. Public Sub _new(centerX As Integer, centerY As Integer, radius As Float)
  2.  
  3.  
I have made a little example and added some properties, a method and a function, so you can see data was transferred and stuff gets done. Just run in IDE and look in the console what happens, then study and ask questions if needed, right here. It's made in 3.19.6, if you have a lower version just do a compile all if IDE complains.  ;)

Attachment

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
Yes a class is not made by code (methods/functions).
A class is defined by a separate file with a name, eg. OPC.class

And as gbWilly says the special method _new()  /lang/special/new - Gambas Documentation is used.

the _new method is called for a "creatable" class when you use the New keyword to create an object like this…
Dim hOPC As OPC = New OPC(CenterX, CenterY, Radius)

If you want to use OPC.class directly without creating an object you should look into Static /lang/static - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Now let us discuss what a "class" is, what it's attributes, methods¸ signals (events) are and what it is not.
As others have raised, a class is no something that you can "create" on the fly (well it can be, but not until postgrad work). A class for want of a better word is a "specification" of a set of organisms whose existence can come and go as required by the universe. Each star is an "instance" of the class "star" but it is not "the" star. A dog is not "the" animal, but it is "a" animal as in an instance of the class animal with 4 legs that says "woof".
I attach the world famous but rotten Animals project for your enjoyment.
Attachment

Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
DIYTinkerer is in the usergroup ‘Regular’
Ah got-it, the penny has dropped :-) Thanks so much :-)
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
DIYTinkerer is in the usergroup ‘Regular’

thatbruce said

Now let us discuss what a "class" is, what it's attributes, methods¸ signals (events) are and what it is not.
As others have raised, a class is no something that you can "create" on the fly (well it can be, but not until postgrad work). A class for want of a better word is a "specification" of a set of organisms whose existence can come and go as required by the universe. Each star is an "instance" of the class "star" but it is not "the" star. A dog is not "the" animal, but it is "a" animal as in an instance of the class animal with 4 legs that says "woof".
I attach the world famous but rotten Animals project for your enjoyment.
animals-0.0.1.tar.gz
Thanks - this is also very helpful - I have been thinking of classes slightly wrong -
Online now: No Back to the top
1 guest and 0 members have just viewed this.