Results 1 to 4 of 4

Thread: MyClass & MyClassCOM - simple demo of creating module class

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735

    Lightbulb MyClass & MyClassCOM - simple demo of creating module class

    Thanks to fact Eros is dark magician, ThinBASIC modules can define not just equates, variables, UDTs and functions... they can define classes too. And what is the best about it? You can define classes even from languages which have no OOP support.

    The attached file contains:
    • source code for thinBasic_MyClass module - it shows how to define class using custom memory allocations (universal, possible from any language)
    • source code for thinBasic_MyClassCOM - it shows how to define class by wrapping COM object (preferred way if you are PowerBASIC programmer)
    • example scripts which use MyClass and MyClassCOM sample classes


    To make it simple and the most illustrative, the MyClass module is kept very simple:
    • it has constructor, which can optionally take one parameter
    • class internally stores one number, which you can read or write using get/set method
    • that is all


    Of course, more powerful classes can be done, but to introduce you softly to module developement of classes, this is the way to keep it easy to understand.

    Here is test code of the classes - you can see using objects in ThinBASIC is intuitive and nothing to be afraid of!
    Uses "MyClass", "Console"
    
    Dim a As MyClass     
    Dim b As MyClass 
    
    Print  "Instantiating object a..."
    a = New MyClass ' -- No parameter specified, MyClass will hold default -1 (defined in module)               
    PrintL "DONE"
    
    Print  "Instantiating object b..."
    b = New MyClass(789) ' -- Some values can be passed to constructor, it is up to the author of class if this is allowed or not               
    PrintL "DONE"
    
    PrintL "Default value inside a:", a.Get()
    PrintL "Default value inside b:", b.Get()
    
    PrintL "Setting new values for a and b..."
    
    a.Set(123)                             
    b.Set(456)                             
    
    PrintL "Value inside a now:", a.Get()
    PrintL "Value inside b now:", b.Get()
    
    PrintL
    PrintL "Press any key to quit..."
    WaitKey
    
    I hope you will like it. There is more to class developement, but more about it in future.


    Petr
    Attached Files Attached Files
    Last edited by Petr Schreiber; 20-05-2013 at 23:14.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,549
    Rep Power
    173
    So I can not "Free" or "Kill" some class already?
    And does it need to specify the Index of class when creating a New one or is this optional or is this no Index but some flag inside parens?

    Just for fun and better readability I re-arranged the thincore.inc a little bit. Why do we have those nice commenting-functions in thinAir?
    Attached Files Attached Files
    Last edited by ReneMiner; 20-05-2013 at 21:07.
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735
    Hi Rene,

    it is not index of class, it is parameter of constructor. Imagine Vector class - it could take as parameter the X, Y, Z coordinates...
    It is up to programmer whether the constructor has parameters or not.

    Object variables are killed once the run out of scope... You could add Free method if you want to do that earlier.


    Petr
    Last edited by Petr Schreiber; 20-05-2013 at 23:17.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    Looks easy,
    thank you for the tiny examples.

    Joshy
    (Sorry about my bad English.)

Similar Threads

  1. creating my own class
    By ReneMiner in forum thinBasic General
    Replies: 4
    Last Post: 17-11-2012, 20:07
  2. Prompt > Simple console demo of word whisperer
    By Petr Schreiber in forum Console: source code examples for beginners
    Replies: 4
    Last Post: 11-07-2012, 21:59
  3. MetaCircles - simple entity demo
    By Petr Schreiber in forum TBGL module by Petr Schreiber
    Replies: 5
    Last Post: 25-07-2011, 00:15
  4. Minimal example of module class
    By Petr Schreiber in forum thinBasic SDK
    Replies: 3
    Last Post: 07-07-2011, 07:25
  5. GCC module example: very very simple expression evaluator
    By ErosOlmi in forum Module SDK (GCC C /C++)
    Replies: 4
    Last Post: 15-09-2007, 21:30

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •