Page 1 of 1

Functions and Classes

Posted: Sun Jan 10, 2010 2:19 am
by Hacker
Hello hello hello !!!!!

Could someone tell me please as are used in functions and classes coolbasic.

Thank you very much for your help ...


Re: Functions and Classes

Posted: Sun Jan 10, 2010 4:23 am
by Jare
There are no classes in CoolBasic. But atleast there are types that create instances that store multiple properties.

First things first, functions can be used like this

Code: Select all

a = 50
b = 40
MakeError MyFunc(a,b)

'Define function name and parameter variables. Use $ to define a parameter as string or # to define as float.
Function MyFunc(number1, number2, textstring$ = "Sum: ") 'Optional parameters can be defined by setting a value to a parameter using = operator. The default value must not be an expression. Only simple values will work.
    'Normal statements inside the function
    'NOTE! Variables a and b DO NOT have any value in this function. If you use them inside the function, they are handled as completely different variables than the ones outside the function.
    'If you want to use outsider variables inside a function, you must define the variables as global outside the function block using the following syntax: Global SomeGlobalVariable.
    sum = number1 + number2
    'Return value from function
    Return textstring$ + sum


A type is a list of objects (=instances of the type). Each instance has properties defined by the type. The properties can be of any data type but not an array. Types differ from classes by lack of support for methods or functions. But they are still the closest way to implement something like classes that CoolBasic has to offer.

Code: Select all

'Define a type
Type User
    'Define properties for the type
    Field name As String
    Field age 'Field without data type definition will be of type Integer
    'You can have as many fields (properties) as you like. Atleast I haven't found any limit for the number of fields in a type.

'Create an instance of the type
u.User = New(User)
'There are two thins to consider in the above statement:
'First we declare a variable named u.User which means that the varible will be [i]a pointer to a type named User[/i].
'Secondly the new instance will be created and assigned to the variable by the New() function call.

'Set values to our type instance
u\name = "George Smith"
u\age = 52
'Note that after the pointer variable u is defined you do not have to use the long format (u.User) of the variable - you can use the short format (u) instead.
'Character \ is used to get access to a instance's properties.

'Let's create another user
u = New(User)
u\name = "Alice Armstrong"
u\age = 28

'Now we have two type instances. We can use the For ... Each statement for example to read information about the instances:
For u = Each User
'The for loop takes a type pointer variable, = operator and after keyword Each name of the type. Note that the variable must be assigned for the given type (or you can assign it in the For ... Each statement by using u.User instead of u)!
    Print u\name + " "+u\age
Next u

'Now that you can create and go through instances, you should also know how to delete them.
'Delete the first instance
first_user.User = First(User)
If first_user <> NULL Then Delete first_user
'Delete command is used to delete type instances.
'Function First() returns the first instance of a given type. Note that if the type contains no instances, the function returns NULL. In this case you must not try to use the instance in any way! Otherwise you will get Memory Access Violation error message (yeah the sadly famous one).

'Now the second (and last) instance is also the first instance in the type. Delete it also:
another_user.User = First(User)
If another_user <> NULL Then Delete another_user

'Here are some other functions that may become handy when dealing with type instances:
u.User = Last(User) 'Like First() but returns the last instance
u = After(u) 'Returns an instance right after the given instance
u = Before(u) 'Returns an instance right before the given instance
'Note that all of those functions can return NULL!

'Still one thing about variables pointing to type instances: they ARE NOT regular variables - the cannot be passed as parameters to functions and they cannot be written into a file or to be assigned to REGULAR variables, arrays or into another type instance's fields.
'But you can convert them to intergers and back to pointers. The converted integers can be used more flexible.
u.User = New(User)
u\name = "Matt Jones"
u\age = 44
id = ConvertToInteger(u)
'ConvertToInteger)() function converts type instance pointer to an integer. You can even print the returned value if you want to see it:
Print id 'Will print out a random looking integer value

'Pass the instance to a function

Function PrintName(id)
    u.User = ConvertToType(id)
    'ConvertToType() returns a type instance from the given integer value.
    Print u\name