PDA

View Full Version : Declaring New Classes



Benjamin
23-12-2023, 19:35
There doesn't seem to be any documentation or sample programs for thinBasic_Class_Add and thinBasic_Class_AddMethod. I would like to know how to declare classes in ThinBasic, something along the lines of:


class cell is
var contents: Integer := 0;
method get(): Integer is
return self.contents;
end;
method set(n: Integer) is
self.contents := n;
end;
end;

Any suggestions? Am I missing something?

ErosOlmi
24-12-2023, 13:11
Yes documentation is not the max regarding classes and objects
Classes in thinBasic are an extension of TYPE

Here an example hope can give a start:


uses "console"


Type tCell
Content as Long

function Content_Get()
function = me.Content
end function

function Content_Set(byval c as Long) as long
function = me.Content '---Retirn previou value before changes
me.Content = c
end function
end type


'---Define a new objwct of class/type tCell
Dim Cell as tCell
printl "Assigning Cell.Content_Set(10)"
Cell.Content_Set(10)
printl "Cell.Content_Get = ", Cell.Content_Get
printl
printl "thinBasic has no Private/Public properties so properties inside a type can be accessed directly"
printl "Assigning Cell.Content = 10"
Cell.Content = 12
printl "Cell.Content = ", Cell.Content


WaitKey

ErosOlmi
24-12-2023, 13:14
Have also a look at Petr Schreiber help on TYPEs

https://help.thinbasic.com/index.html?petr_schreiber_online_help_on_.htm

Benjamin
24-12-2023, 14:24
Thank you so much Eros. I didn't really understand, but chatGPT did. He summed it up thusly: "In summary, this code snippet shows how to create a type with methods in thinBasic to mimic a simple class. It also highlights that properties within a type can be accessed directly, as thinBasic lacks the concept of private/public properties."