View Full Version : Doubt about classes in another module
Hi all,
I was playing with some classes. This is my class file.
Class MyClass
Dim a As Integer = 1500
Dim b As String = "This is from class"
Method MyFunc(ByVal c As Integer)
Return C * 10
End Method
End Class
I saved this file as MyClass1.inc in ThinbasicPath\Mod.
And this is my working file.
Uses "Console"
Module "MyClass1.inc"
Dim My_cls As New MyClass1
Print My_cls.b
Print "====================="
Print My_cls.a
Print "====================="
Print My_cls.MyFunc(5)
WaitKey
This is saved as ClassTest.tbasicc in another directory. When i execute this script, the console came and vanish very speed. What is wrong with this script. I have test this script with "Uses" instead of "Module" also.
Note: All i want is to create a class and save it in a module. Then use that class in another script.
ErosOlmi
04-11-2016, 16:23
Dear kcvinu,
thinBasic pseudo OOP is a little different from standard OOP languages that use Classes/Methods/Instance variables.
thinBasic has used TYPEs to implement a sort of OOP. So you have to use TYPE/END TYPE
Correct possible script based on your code can be the following:
'---Load console Module
Uses "Console"
'---Type definition
Type MyClass
'---Local variables
a As Integer
b As String
'---Default constructor.
'---In this special functions can take place initialization of the type variables
Function _Create()
Me.a = 1500
Me.b = "This is from class"
End Function
'---Type method
Function MyFunc(ByVal c As Integer) As Long
Return C * 10
End Function
End Type
Dim My_cls As New MyClass
PrintL My_cls.b
PrintL "====================="
PrintL My_cls.a
PrintL "====================="
PrintL My_cls.MyFunc(5)
WaitKey
You can put you TYPE/END TYPE declaration into a separate file and use #INCLUDE to include its source code in many different scripts. See #INCLUDE help to have more info on how to use it.
Example
'---Load console Module
Uses "Console"
#INCLUDE "MyClassIncludeFile.tbasic"
Dim My_cls As New MyClass
PrintL My_cls.b
PrintL "====================="
PrintL My_cls.a
PrintL "====================="
PrintL My_cls.MyFunc(5)
WaitKey
You can place your include files in any directory you want but you need to tell in your script where to load them. Usually included files are placed into a sub directory of your project and you can use partial include dir like:
#INCLUDE ".\IncludeDirectory\MyClassIncludeFile.tbasic"
Instead Modules are not source code but compiled DLL specifically developed for thinBasic using thinBasic SDK.
Modules are loaded into the script using the keyword USES
Ciao
Eros
Hi ErosOlmi,
Thanks a lot. Well, that worked fine. I am learning tb now. :)
But when i search in hep file for keyword "_Create", i dont get any result. That means these infos are not in help file. Kindly suggest me some links or pages where i can learn about these functions.
Anyway, i am eager to wait for your unicode aware version of ThinBasic.
ErosOlmi
05-11-2016, 10:49
Yes, my fault. Pseudo OOP in thinBasic is quite recent and still had no time to create proper help material.
Have a look at \thinBasic\SampleScripts\OOP\ directory for some script examples
Regarding unicode version, I'm working on it. Already have something working but I first need to finish thinAir (editor) migration from old edit control (CodeMax) to new unicode aware text control Scintilla.
Anyway, hope to have something to be released for test in few weeks.
Ciao
Eros
Hi ErosOlmi,
Its ok. No problem. If you don't mind, I am willing to do some documentation work. And it's a glad news that you are planning to use Scintilla. Its very good and customizable. By using it, we can get intellisense and auto complete also. Well, if you don't mind, i have some feature requests.
1. When hitting enter key after typing a line with keywords like "If, For, Do, While, Select, Function, Type, Sub" automatically add the end appropriate end keywords like "End If, Next, Until, Wend, End Select, End Function, End Type, End Sub".
ErosOlmi
05-11-2016, 20:17
Yes, I will add this as an option.
Actually you can already do it in thinAIR using right click and choosing "Insert code block ..."
Code blocks can also be customized with personal code blocks. Check inside \thinBasic\thinAir\Syntax\thinBasic\
Files thinBasic_Blocks_Usr.ini and thinBasic_Blocks.ini
thinBasic_Blocks_Usr.ini is the user specific version and will never be replaced in future thinBasic versions
thinBasic_Blocks.ini is the official one and will be replaced when thinBasic is updated.
Hi ErosOlmi,
Thanks. That is a good feature. Especially Callback function. But how can i tell ThinAir to rest the cursor in my favourite position ?
ReneMiner
07-11-2016, 01:48
Hi guys,
Regarding "If" and automatic adding "End If" etc. I already tried when I created thinICE.
I stumbled over the following: when editing an existing Script and using enter-key then the editor added more "End If" even if none required. So I came to the conclusion to make it different and wait for the user to type "End" & $Spc and the editor adds the required "If", "Const", 'Type", "With", "Function", "Select" or whatever is currently needed when the space is typed in
@ReneMiner,
I think if you are going to add that feature, you need to apply this pseudo code.
When user press an enter key
1. get the current line number & line text from scite editor control.
2. Get the first word and last word from line text.
3. Get the tab position of first word.
4. If first word is "If" then check the last word for the presence of a "Then". If it is not there, then add "Then".
5. If first word is "Function" then check the last word for the presence of a "()". If it is not there, then add "()".
6. Find appropriate end keyword for the first word.
7. Loop through all lines from current line number + 1. Check each line's specific tab position for the presence of the appropriate end key word. If there is not an end key word, add one and rest cursor in a specific position. If there is an end keyword, just add another line under current line.
ReneMiner
08-11-2016, 19:57
It could work if the user cares for exact intended lines. However if the line Starts with "if" and it does not end with "then" there are different causes. Either it's a one-line if-statement, the user decided to make use of line-continuation - a special Feature of thinBasic)or the user simply forgot it
C-users or purebasic-users are a group them will happen this very often ;)
For me typing "end" and spacebar and the editor selects the correct Keyword that has to follow seems quite ok
@ReneMiner,
Anyways, i am a visual studio + vb.net fan. So i like the features vs offers. Its easy to learn programming with an IDE like vs. Every programming language should have an IDE like this.