reading on the internet we refer to FreeBASIC as an object-oriented language, where it is possible to create primitive classes and use them as real objects, I've seen some examples but I can't understand how they work also because when I go to compile always gives me some mistakes.
this is an example of my code where I try to understand how use it
type MyClass
dim mystring as string
function myfunction()as string
dim myout as string
myout = "Hello your message are: " + this.mystring
return myout
end function
end type
dim myobject as myclass
myobject.mystring = "Im noob"
print myobject.myfunction
But in this way doesnt work
Unfortunately FreeBASIC has an inelegant class management and with some limitations, when you create a class you cannot put the functions or subs inside but you can declare them and build them later outside the type, below I wrote you a source code where it shows how it is structured, when you create a function declared inside the class it must be preceded by the name of the class separated by a dot
'This one define "IsEven(value)"
'that give true or 1 if the vaule n are Even number
#define IsEven(n) ((n AND 1) = 0)
'I make a class type where inside are 2 vars and 2 functions
type Useless
' this is the vars as integer
dim StartNum as integer
dim CurrentNum as integer
' this are the functions declared we ave to declare the function who work inside the class
declare function EvenValue(byval value as integer)as integer
declare function OddValue(byval value as integer)as integer
declare function FinalValue()as string
end type
' After declare the function inside the type (class) we ave to use the
' name of type followed with the name of the function separated by dot.
'
' inside type "declare myfunction()as integer"
' the function can be string too or other type
'
' when close the type with "End Type" you can make the function
' function nameclass.myfunction()as integer
function Useless.EvenValue(byval value as integer)as integer
dim Result as integer
Result = (value / 2)
return Result
end function
function Useless.OddValue(byval value as integer)as integer
dim Result as integer
Result = ((value * 3) + 1 )
return Result
end function
function Useless.FinalValue()as string
'with this you can use every function, var or sub
'inside the class(type)
'watever name you used for the object
dim OutMess as String
OutMess = "Start value =" + str(this.StartNum) + " ---- End value =" + str(this.CurrentNum)
return OutMess
end function
'this are global var outside of class(type)
dim thenexit as integer = 0
'this is the object created as class
dim Calculate as Useless
'message text and store imput inside the public integer var inside the object
print "type a number"
input Calculate.StartNum
'you can change value taked from other var from the object
Calculate.CurrentNum = Calculate.StartNum
'We use the do until cicle because we know at last we ave the one value
do until(thenexit > 0)
if IsEven(Calculate.CurrentNum) then
'we use the object.function for even value
Calculate.CurrentNum = Calculate.EvenValue(Calculate.CurrentNum)
else
'we use the object.function for odd value
Calculate.CurrentNum = Calculate.OddValue(Calculate.CurrentNum)
end if
print Calculate.CurrentNum
if Calculate.CurrentNum = 1 then
'and at the end we use the function with shared vars
print Calculate.FinalValue
thenexit = 1
end if
loop