Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
an "overloaded subroutine" example
#1
an example of a means to have overloaed subroutines or at least subroutines with a varying range of variables. 
it makes use of a modified version of splitstring that someone else developed that allows the variables to be broken down in the subroutine.

Code: (Select All)
'an example of an overloaded subroutine in QB64
'
'more acurately an example of passing a different range of variables to a sub
'and gettign a different range of results based on those arguments
'
'$Dynamic
Screen _NewImage(500, 240, 256)
Dim Shared klr
Locate 1, 1: Print "A dot      (press Any key) "
overload_example "12"
overload_example "100,100"
Sleep
Locate 1, 1: Print "                            "
Locate 1, 1: Print "Amother dot (press Any key) "
overload_example "50,50,14"
Sleep
Locate 1, 1: Print "                            "
Locate 1, 1: Print "A Filled Box (press Any key)"

overload_example "50,50,100,100,10"
Sleep
Locate 1, 1: Print "                            "
Locate 1, 1: Print "Outlined that box "

overload_example "50,50,100,100"
Sleep
Cls
Locate 1, 1: Print "                            "
Locate 1, 1: Print "The code is an example of passing numerical variables "
overload_example "30,30,110,110,2"
For x = 10 To 40 Step 5
    A$ = _Trim$(Str$(70 - x))
    B$ = _Trim$(Str$(x + 70))
    overload_example A$ + "," + A$ + "," + B$ + "," + B$
Next x

End

' overload_example'
'takesa string with a set of arguments delimited by a comma
'if there is one argument the default color is set
'if there are 2 arguments a pixel is drawn in the deafult color with pset
'if there are 3 arguments a pixel is drawn in a temporary color but the default is not changed
'if there are 4 arguments a Box is drawn  the default color
'if there are 5 arguments a Filled Box is drawn  the temporary color

Sub overload_example (argument$)
    Dim argu$(0)
    SplitString argument$, ",", argu$()
    a_count = UBound(argu$)
    Select Case a_count
        Case 1 'set the defined color for follwoign statements
            klr = Val(argu$(1))
        Case 2 'pset in defined color
            x0 = Val(argu$(1))
            y0 = Val(argu$(2))
            PSet (x0, y0), klr
        Case 3 'pset in temporary color
            x0 = Val(argu$(1))
            y0 = Val(argu$(2))
            tklr = Val(argu$(3))
            PSet (x0, y0), tklr
        Case 4 'draw a box in defined color
            x0 = Val(argu$(1))
            y0 = Val(argu$(2))
            x1 = Val(argu$(3))
            y1 = Val(argu$(4))
            Line (x0, y0)-(x1, y1), klr, B
        Case 5 'draw a filled in temporary color
            x0 = Val(argu$(1))
            y0 = Val(argu$(2))
            x1 = Val(argu$(3))
            y1 = Val(argu$(4))
            tklr = Val(argu$(5))
            Line (x0, y0)-(x1, y1), tklr, BF
    End Select
End Sub
Sub SplitString (inputString$, delimiter$, wordArray$())
    'make sure you have dynamic arrays set up
    wordCount% = 0
    startPos% = 1
    Do
        psn% = InStr(startPos%, inputString$, delimiter$) ' Find the next delimiter
        If psn% = 0 Then
            ' No more delimiters found, this is the last word
            word$ = Mid$(inputString$, startPos%)
            If Len(_Trim$(word$)) > 0 Then ' Check for empty word (e.g., multiple spaces)
                wordCount% = wordCount% + 1
                ReDim _Preserve wordArray$(wordCount%)
                wordArray$(wordCount%) = word$
            End If
            Exit Do ' Exit the loop
        Else
            ' Delimiter found, extract the word
            word$ = Mid$(inputString$, startPos%, psn% - startPos%)
            If Len(_Trim$(word$)) > 0 Then ' Check for empty word (e.g., multiple spaces)
                wordCount% = wordCount% + 1
                ReDim _Preserve wordArray$(wordCount%)
                wordArray$(wordCount%) = word$
            End If
            startPos% = psn% + Len(delimiter$) ' Move the starting position past the delimiter
        End If
    Loop
End Sub
Reply


Messages In This Thread
an "overloaded subroutine" example - by James D Jarvis - 05-08-2025, 12:55 AM
RE: an "overloaded subroutine" example - by bplus - 05-08-2025, 07:55 AM
RE: an "overloaded subroutine" example - by bplus - 05-08-2025, 10:20 AM
RE: an "overloaded subroutine" example - by Jack - 05-08-2025, 03:28 PM



Users browsing this thread: 1 Guest(s)