Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How to bit shift in ThinBasic ?

  1. #1
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    120
    Rep Power
    18

    How to use constructor overloading ?

    Hi all,
    I need to use overloaded constructors for RGB type. One ctor will receive a long parameter and another will receive 3 byte parameters. But now thinbasic is stop working when i use two _create functions inside the type.
    Sorry for the title. I can't edit it.
    Last edited by kcvinu; 14-04-2021 at 10:56.

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Hi,

    thinBasic does not support function overloading and/or multiple constructors at the moment.


    Petr
    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

  3. #3
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,422
    Blog Entries
    1
    Rep Power
    157
    the _Create-function is (as _Destroy or _Get and _Set) developed not to be called. They are invoked internally in certain moments and act as events.<br><br>to initialize your types you can create a function named as "Init" or "AfterCreation" / whatever as long it starts with an alphabet char. <br>Underscore might be defined as word/char (\w) in regex but is actually not [A-Za-z]<br>when you want to use a technique as Overloading parameters - its difficult for type-functions to develop but for "normal functions" you can use different approaches to pass different&nbsp; parameter-types either byref or byval to a function that will check what is passed to it and call the dedicated function for the received count and type of parameters.&nbsp;<br>Your friends are keywords as&nbsp;<br><br>Optional<br>As Variant<br>As AnyType&nbsp;<br>...(Any) As ...<br><br>for headline of the function that "borrows" the common shared name to the different functions.<br><br>What you have to decide in advance is if are all parameters to pass byval or all byRef / will you need one byref always or 2 - are they optional or not and haw many will be byval - for the function that uses the less parameters then you can plan how to order these. Alike all child-functions will receive 1 byref parameter always and one optional byref and all will have at least 2 parameters byval and one optional byval ... of course the optional parameters are to pass at the end and non-optional before.&nbsp;<br>ByRef means it works with some memory address that could be passed byval as well if the function knows the type to deal with. And Variants allow to receive different variable types and you can sort them by VariantVT to use either Variant$ and take it as String or Variant# wich will whatever the parameter contains in numeric way.&nbsp;<br><br><br>If its like homogennous parameters in a row consider the use of variadic parameters that will receive an infinite bunch of same type in a row and hold the value as you know from any array. So if there is a parameter that is always present, but it differs how many of those are up to come then this can be a variadic parameter.<br>the actual functions that shall share the same name for the user are still different functions and the main-function - Lets name it DoSomething() will call the correct one depending what the user has passed.<br><br>
    <br><br>Function Do something(ByRef anObject As AnyType,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byval position(ANY) as PointApi,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byval vParam1 as Variant, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;optional byval vParam2 as Variant,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byref pOtherObject As myUDTVar Ptr [,...]) As String<br>&nbsp;<br>' the function seperates now what to process under the name "DoSomething" where depending on the parameters differnt things will be done. this function "knows" the name of itself:<br><br>string thisFunc=FunctionName<br>long passedParametersCount =Function_CParams&nbsp;<br>
    
    <br>&nbsp;it could determine typeOf(anObject) or just pass it further byref<br>&nbsp;the position can be different amount but at least 1 must be given. The variadic parameters must be <br>&nbsp;passed inbetween parenthesis as&nbsp;<br><br>
    <br>Long X=DoSomething(W as tWindow, (P1, P7, P5, P3), "wash")<br>
    
    <br>and&nbsp;<br>
    <br>String IcanSee = DoSomething(B as tButton, (p1,p2), 5, "push")&nbsp;&nbsp;<br>
    
    <br><br>Depending on what TypeOf(anObject)<br>and the CountOf(position)<br>[ Select Case] VariantVT(vParam1)&nbsp;<br>&nbsp; &nbsp; Case %VT_Asciiz, 8 ' we have a string<br>&nbsp; &nbsp; &nbsp; sValue = Variant$(vParam1)&nbsp;<br>&nbsp; &nbsp;Case %VT_Ext ' we have a number<br>...<br>Function_cParams tells how many altogether were passed.<br>when this function passes the received result of the function that it called depending on parameters<br>back&nbsp; to the caller in form of a string/ thinbasic "knows" what the caller expects and even if is a byte or an udt that he might expect - it will be the sequence of bytes that you can hand over in a dynamic string.<br><br>You can not only decide by the count and kind of parameters but as well with the content of some.<br>lets say you make the last non-optional parameter and the ones before to be variants and depending if it is a string-variant-type your delegation-call can be as simple as<br><br><span style="color:#0000cd;"><span style="font-family:courier new;"><strong>Call_IfExists "" &amp; Replace$(Function_Name, "TH", Variant$(vParam1)) (anObject, position )<br></strong><span style="font-family:arial;"><br>and it would call something as DoSomeWashing or DoSomePushing from here</span><strong>&nbsp;<br></strong><span style="font-family:arial;">The double double-quote will preven it from misinterpreting maybe a string or its own function-name in front but bame sure to use the literal string content to create a functions name...i hope contains some ideas for you


    soorry the second time today it does that. i am not in the mood to remove the mess made by some strange html bug na
    Last edited by ReneMiner; 23-04-2021 at 20:13.
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  4. #4
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,422
    Blog Entries
    1
    Rep Power
    157
    before i manually fix it again up there
    i place the thing that will do it whenever it occurs again and pick these html-tags out

     uses "console" ', "inet" '"brains"
     
     printl "copy the text onto clipboard and press any key to continue, [esc] to abort"
     if waitkey()="[ESC]" then stop  
     
    string sTextIn =Replace$( clipboard_getText, "<br>" , CRLF)
    
    ' string sTextIn = inet_urlgetstring( "https://www.thinbasic.com/community/showthread.php?13111-How-to-bit-shift-in-ThinBasic&p=96021&viewfull=1#post96021") ' <<< not working...
    ' sTextIn = replace$(sTextIn, "<br>", CRLF)
    
     sTextIn = remove$(sTextIn, "&nbsp;") 
     sTextIn = remove$(sTextIn, "&amp;") 
     local char(lenf(sTextIn)) as byte At StrPtr(sTextIn)
    byte bOpen = asc("<")
    byte bClose = asc(">")
      
    long lStart
                          
    do
       
          select case  Array scan char(lStart+1), =bOpen         
          case 0
                ' get all until the last and be done     
                print Memory_get(strptr(sTextIn) + lStart, strptrlen(strptr(sTextIn))-lStart )
                exit do
          case 1 
             
            ' no chars inbetween   lStart and lStart +1 
            lStart += 1  
          case else 
            print memory_get(strptr(sTextIn)+lStart,Selectexpression-1)     
            lStart += Selectexpression 
         end select    
         if lStart < ubound(char) then lStart += array scan char(lStart+1) , =bClose
     
    loop until lStart >=Ubound(char)
    printl
    printl "any key to end"
    waitkey
    
    Last edited by ReneMiner; 23-04-2021 at 23:19.
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  5. #5
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    120
    Rep Power
    18
    @Rene Miner & @Petr
    The one and only easy workaround in front of me is this.
    type Sample
      iVal As Integer
      sVal as String
      bVal As Boolean
      InitSample1 As Function
      InitSample2 As Function
    End type
    
    function Sample.InitSample1( iv As Integer, sv as String)  
      With Me
        .iVal = iv
        .sVal = sv
      End with  
    End function
    function Sample.InitSample2( iv As Integer, sv as String, bv As Boolean)  
      With Me
        .iVal = iv
        .sVal = sv
        .bVal = bv
      End with  
    End function
    
    Dim sp as Sample
    sp.InitSample1( 25, "Vinod")
    printl("sVal is = ", sp.sVal)
    
    I hope you people can make thinBasic better.

  6. #6
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    120
    Rep Power
    18
    @Rene Miner,
    (as _Destroy or _Get and _Set)
    When I read this words from your comment, I suddenly typed "Set" in thinAir and it recognize it as a keyword. But Then I search in Sample script folder to find an example with "Set" & "Get", but I couldn't find any. Could you please show me an example ?
    @Petr,
    How to write a property in an UDT ? The "Private" keyword is making no difference in script. Does the UDT section in thinBasic is incomplete ?

  7. #7
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,422
    Blog Entries
    1
    Rep Power
    157
    you can do something as

    Function XYZ_NNNS(N1 as Number, N2 As Number, Really As Boolean, Ofcourse As String) As String
    End Function
    
    Function XYZ_SNNl(sData As String, N1 as Number, N2 As Number) As String
    End Function
    
    Function XYZ_SS(s1 As String, S2 As String) As String
    End Function
    
    Function XYZ_N(N as Number) As String
    End Function
    
    Function XYZ(V1 As Variant, optional V2 As Variant, V3 As Variant, V4 As As Variant) As String
    
    
    String par$(4)
    String sResult, suffix ="_"
    
    'find out if V1 is String or Number:
         if$(VariantVT(V1)=%VT_BSTR  then
           ' a string was passed here
            par$(1) = variant$(v1)
            ' so keep it as string and built the functions name 
            ' that shall be called as soon we have all parameters
            suffix &= "S" 
          Else
    '          first parameter is a number/ numeric value. We convert it to its 
    '          binary representation in appearance of a dynamic string:
            par$(1) =MKNumber$(variant#(v1))
    '          remember the parameter is numeric
               suffix &= "N"
          Endif
    ' all other parameters are optional so test if we received more than one:
       If Function_CParam > 1 then
    '     same as above with the second parameter now
              if$(VariantVT(V2)=%VT_BSTR  then
                  par$(2) = variant$(v2)
                  suffix &= "S" 
            Else
                 par$(2) =MKNumber$(variant#(v2))
                 suffix &= "N"
            Endif
            if function_cParam > 2 then ' if more pARameters, check them...
                  if$(VariantVT(V3)=%VT_BSTR  then
                       par$(3) = variant$(v3)
                       suffix &= "S" 
                 Else
                      par$(3) =MKNumber$(variant#(v3))
                      suffix &= "N"
                 Endif
                 if function_cParam > 3 then   ' another parameter_ ok,...
                      if$(VariantVT(V4)=%VT_BSTR  then
                             par$(4) = variant$(v4)
                             suffix &= "S" 
                      Else
                             par$(4) =MKNumber$(variant#(v4))
                             suffix &= "N"
                       Endif   
                       Call_IfExists "" & "XYZ" & suffix( par$(1), par$(2), par$(3), par$(4) ) To sResult
                 Else 
                    Call_IfExists "" & "XYZ" & suffix( par$(1), par$(2), par$(3) ) To sResult
                 EndIf
              Else 
                 Call_IfExists "" & "XYZ" & suffix( par$(1), par$(2)) To sResult
              EndIf
         Else
            Call_IfExists "" & "XYZ" & suffix( par$(1) ) To sResult
         Endif
    
       ' finaly deliver the result, no matter what it is just pass it back what we captured as returned value
       ' if a string came then it will be a string. if a number came then the recipient will expect a number and       ' receive it in a numeric variable where it will be a number. Ony if we had to finalize something here
      'it were the
         Number nResult=CVNumber(sResult)
         ' to calculate with nResult is a bit simpler... 
      Function = sResult
    End Function
    
    you see XYZ() as the function without parameters, actually it would accept as many as the most
    of the ones above that is the one accepting 3 Numbers and a String, and minimum 1 parameter - the last before function XYZ, accepting 1 number.

    depending on the parameters, - internal you dont need to worry about type-conversions -
    only when you pass to or get values from a declared function it must match. In thinbasic
    you can actually use dynamic string for anything. Except it is more than 2GB...

    You will call XYZ(...how many parameters you require...) only
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  8. #8
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    120
    Rep Power
    18
    @Rene Miner,
    Wow !! That's a great approach. Thanks for the code sample.
    Could you please elaborate a little more about "Set" & "Get" ?

  9. #9
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,422
    Blog Entries
    1
    Rep Power
    157
    _Get and _Set ( the underscore!!!) is for classes, i.e. udts with functions and classes are a protected - you must not write a _Get nor _Set-function to your udt since these exist already. They are to process what will
    happen if you use any assignment-operators that end with =
    If +=, -=, *=, /=, \=, &= or = only it means -depending on the position of your UdtName left or right of it-

    either

    myUDT.xxx {=|+=|-=|*=|/=|\=|&=} expression

    means actually internal will be processed something as the interpreter encounters this line

    myUDT... will lookup for token myUDT and find it a variablename of an udt, defined as "<someUDT>"
    . dot, something defined inbetween the Type <someUDT> and End Type is about to follow
    xxx a property, method or a static udt-member? it will detect what it is...\
    .= assignement operator (excludes xxx being a type-function / method already)...

    expression will be "operated" in _Set-mode with myUDT.xxx, depending now if xxx is a static (a property that all defined as <someUDT> will have in common) or an individual property
    to make it short it will do like:

    Memory_Set( VarPtr(myUDT) + udtElement_Offset(<someUDT.xxx>), evaluatedExpression)

    and if its vice versa, the UDT on the right hand side of the assignment:

    var_or_func {=|+=|-=|*=|/=|\=|&=} myUDT.xxx

    means that var_or_func is to be assign-operated with what it will retrieve using

    Memory_Get( VarPtr(myUDT) + udtElement_Offset(<someUDT.xxx>), sizeOf(TypeOf(myUDT.xxx)))

    So you don't need to write a _Get nor _Set for the udt, its already in there that you can just use the equal-sign= and even perform a calculation in one go using one of the other assignment-operators.



    PS. The script above contains typos. i can no more edit since i posted again here, there's something as the function XYZ_SNNN1 or similar - remove the trailing 1


    and the lines starting IF$( 3 or 4 times) to remove is "$("

    Last edited by ReneMiner; 26-04-2021 at 16:23.
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  10. #10
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    120
    Rep Power
    18
    @Rene Miner,
    Thanks for the details reply. So yeah, I miss-understood the concept behind _Set & _Get. I thought it is something like this VB .Net code.
    Public Class Sample
    	Private _SomeValue As Integer
    	Public Property SomeProp() As Integer
    		Get
    			Return Me._SomeValue
    		End Get
    		Set(iVal As Integer)
    			Me._SomeValue = iVal
    			'// Do some other operations. // thinBasic lacks this.
    		End Set
    	End property
    End Class
    
    '// Usage
    Dim MySample As New Sample
    MySample.SomeProp = 1500  '\\ This line is more than just an assignment.
    
    I am seeking a similar feature in ThinBasic.

Page 1 of 2 12 LastLast

Similar Threads

  1. Is there a bit-shift function?
    By ISAWHIM in forum thinBasic General
    Replies: 18
    Last Post: 29-09-2008, 17:05
  2. For SHIFT keyword
    By ErosOlmi in forum Samples for help file
    Replies: 0
    Last Post: 26-05-2007, 10:25

Posting Permissions

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