Code: Select all
;==================================================================
; ### Example to use variable parameters for Procedures ###
; Author: Thomas <ts-soft> Schulz
; Date: Mrz 16, 2012
; Version: 1.0
; Target OS: All
; Target Compiler: PureBasic 4.40 and later
;==================================================================
EnableExplicit
Structure V_ARG
Type.l
StructureUnion
a.a
b.b
c.c
d.d
f.f
i.i
l.l
*s.String
q.q
u.u
w.w
EndStructureUnion
EndStructure
Procedure V_ARG_Add(Array V_ARG.V_ARG(1), Type.l, *value)
Protected index = ArraySize(V_ARG())
Protected *v.Quad = *value
ReDim V_ARG(index + 1)
With V_ARG(index)
\Type = Type
\q = *v\q
EndWith
EndProcedure
; example
Procedure Foo(Array V_ARG.V_ARG(1))
Protected i
Protected count = ArraySize(V_ARG())
Debug "Count of parameters: " + Str(count)
Debug ""
For i = 0 To count - 1
With V_ARG(i)
Select \Type
Case #PB_Ascii
Debug "Parameter " + Str(i + 1) + " = UByte, Value = " + Str(V_ARG(i)\a)
Case #PB_Byte
Debug "Parameter " + Str(i + 1) + " = Byte, Value = " + Str(V_ARG(i)\b)
Case #PB_Character
Debug "Parameter " + Str(i + 1) + " = Char, Value = " + Str(V_ARG(i)\c)
Case #PB_Double
Debug "Parameter " + Str(i + 1) + " = Double, Value = " + StrD(V_ARG(i)\d)
Case #PB_Float
Debug "Parameter " + Str(i + 1) + " = FLoat, Value = " + StrF(V_ARG(i)\f)
Case #PB_Integer
Debug "Parameter " + Str(i + 1) + " = Integer, Value = " + Str(V_ARG(i)\i)
Case #PB_Long
Debug "Parameter " + Str(i + 1) + " = Long, Value = " + Str(V_ARG(i)\l)
Case #PB_Quad
Debug "Parameter" + Str(i + 1) + " = Quad, Value = " + Str(V_ARG(i)\q)
Case #PB_String
Debug "Parameter " + Str(i + 1) + " = String, Value = " + PeekS(V_ARG(i)\s)
Case #PB_Unicode
Debug "Parameter " + Str(i + 1) + " = UWord, Value = " + Str(V_ARG(i)\u)
Case #PB_Word
Debug "Parameter " + Str(i + 1) + " = Word, Value = " + Str(V_ARG(i)\w)
EndSelect
EndWith
Next
EndProcedure
Dim para_array.V_ARG(0)
Define Text.String
Text\s = "Feel the ..Pure.. Power"
V_ARG_Add(para_array(), #PB_String, @Text)
Define pi.d = #PI
V_ARG_Add(para_array(), #PB_Double, @pi)
Define word.w = 65535
V_ARG_Add(para_array(), #PB_Word, @word)
V_ARG_Add(para_array(), #PB_Unicode, @word)
Foo(para_array())
