Page 1 of 1

Creating a class requirig mandatory parameters

Posted: Mon Jun 09, 2025 5:23 pm
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?

Re: Creating a class requirig mandatory parameters

Posted: Mon Jun 09, 2025 11:17 pm
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. ;)

Re: Creating a class requirig mandatory parameters

Posted: Tue Jun 10, 2025 7:46 am
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

Re: Creating a class requirig mandatory parameters

Posted: Tue Jun 10, 2025 9:55 am
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

Re: Creating a class requirig mandatory parameters

Posted: Tue Jun 10, 2025 12:49 pm
by DIYTinkerer
Ah got-it, the penny has dropped :-) Thanks so much :-)

Re: Creating a class requirig mandatory parameters

Posted: Tue Jun 10, 2025 12:51 pm
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 -