View Full Version : How to bit shift in ThinBasic ?
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.
Petr Schreiber
23-04-2021, 09:59
Hi,
thinBasic does not support function overloading and/or multiple constructors at the moment.
Petr
ReneMiner
23-04-2021, 20:10
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 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. <br>Your friends are keywords as <br><br>Optional<br>As Variant<br>As AnyType <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. <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. <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> Byval position(ANY) as PointApi,<br> Byval vParam1 as Variant, <br> optional byval vParam2 as Variant,<br> Byref pOtherObject As myUDTVar Ptr [,...]) As String<br> <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 <br><br> it could determine typeOf(anObject) or just pass it further byref<br> the position can be different amount but at least 1 must be given. The variadic parameters must be <br> passed inbetween parenthesis as <br><br>
<br>Long X=DoSomething(W as tWindow, (P1, P7, P5, P3), "wash")<br><br>and <br>
<br>String IcanSee = DoSomething(B as tButton, (p1,p2), 5, "push") <br><br><br>Depending on what TypeOf(anObject)<br>and the CountOf(position)<br>[ Select Case] VariantVT(vParam1) <br> Case %VT_Asciiz, 8 ' we have a string<br> sValue = Variant$(vParam1) <br> 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 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 "" & 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> <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
ReneMiner
23-04-2021, 23:17
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, " ")
sTextIn = remove$(sTextIn, "&")
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
@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.
@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 ?
ReneMiner
25-04-2021, 16:02
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
@Rene Miner,
Wow !! That's a great approach. Thanks for the code sample.
Could you please elaborate a little more about "Set" & "Get" ?
ReneMiner
26-04-2021, 16:15
_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 "$("
@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.
ReneMiner
26-04-2021, 20:01
Private and Public are working bit different as in .net
You will use it within type/end type to grant local or global access to the properties or methods of a type but not write it in front of everything but acting as a switch.
Its like
Type tExample
'Public '(default setting / not needed here)
X As Double
Y As Double
Z as Double
Private ' <<< all that follows until "Public" or "End Type" is from now private
A As Long
B As Long
Function DoSomethingPrivate()
End Function
Public ' <<< switch to end private scope
Function DoSomethingPublic(ByRef lA As Long, ByRef lB as Long )
' here accessing Me.A or Me.B is possible
Me.A= lB - lA
Me.B = lB + lA
since keyword "Me" signals a reference of tExample is accessing
' its own data only while
lA = Me.A
lB = Me.B
' were an illegal exploit since that would not obey the rules of privacy
' because its not only giving direct access to the variables but also
' tells theirs position in memory. Would lead to runtime error
end function
end type
Private
' can not be private outside of type declarations
Am not sure if completely developed yet but if your code follows the rules
it will be compatible
@Rene Miner,
Am not sure if completely developed yet
I think that's the reason. Currently, "Private" is making no difference.
Petr Schreiber
02-06-2021, 11:06
Hi,
private / public are reserved keywords, but they have no effect at the moment.
Petr
gintibande
22-04-2022, 15:09
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 ?
Petr Schreiber
22-04-2022, 22:34
Hi gintibande,
I would say the most complete docs for UDT are currently available here:
https://thinbasic.github.io/book-thinbasic_user_defined_types/
We are working on improving the help file docs to include this information.
Thanks for understanding.
P