Creating a class requirig mandatory parameters

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
DIYTinkerer
Newbie
Posts: 3
Joined: Mon Jun 02, 2025 6:59 pm

Creating a class requirig mandatory parameters

Post by DIYTinkerer »

Hi,
I'm trying to create a new class something that requires 3 parameters i.e.

Public centerX As Integer = 400
Public centerY As Integer = 400
Public radius As Float = 400
Public corrector As New OPC(centerX, centerY, radius)


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

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?
User avatar
gbWilly
Site Admin
Posts: 344
Joined: Fri Sep 23, 2016 11:41 am
Location: Netherlands
Contact:

Re: Creating a class requirig mandatory parameters

Post by gbWilly »

DIYTinkerer wrote: Mon Jun 09, 2025 5:23 pm 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:
Public Sub _new(centerX As Integer, centerY As Integer, radius As Float)

End

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. ;)
You do not have the required permissions to view the files attached to this post.
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- Gambas3 Debian/Ubuntu repositories

- GambOS

... there is always a Catch if things go wrong!
User avatar
BruceSteers
Legend
Posts: 2183
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: Creating a class requirig mandatory parameters

Post by BruceSteers »

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() https://gambaswiki.org/wiki/lang/special/new 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 https://gambaswiki.org/wiki/lang/static
Last edited by BruceSteers on Tue Jun 10, 2025 11:55 am, edited 1 time in total.
User avatar
thatbruce
Regular
Posts: 312
Joined: Sat Sep 04, 2021 11:29 pm
Location: Sitting at my desk in South Australia

Re: Creating a class requirig mandatory parameters

Post by thatbruce »

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
You do not have the required permissions to view the files attached to this post.
DIYTinkerer
Newbie
Posts: 3
Joined: Mon Jun 02, 2025 6:59 pm

Re: Creating a class requirig mandatory parameters

Post by DIYTinkerer »

Ah got-it, the penny has dropped :-) Thanks so much :-)
DIYTinkerer
Newbie
Posts: 3
Joined: Mon Jun 02, 2025 6:59 pm

Re: Creating a class requirig mandatory parameters

Post by DIYTinkerer »

thatbruce wrote: Tue Jun 10, 2025 9:55 am 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 -
Post Reply