Shorten your source code by a few thousand characters using concise string formatting functions instead of lengthy concatenation assignments. Works well with user interfaces and error message boxes.
Code is self explanitory and can be easily modified.
Def$( "", "Default string" ) // returns "Default string"
Def$( "1", "2" ) // returns "1"
Def$( a$, b$ ) // returns the value of b$ if a$ is ""; otherwise the value of a$ is returned
Numbers sometimes represent things specified by the user, or is dynamically set to mean something by a script, file or macro. DefStr$ functions prefix the meaning of the number if there is a meaning, otherwise a default meaning is placed to the left of the number. A certain amount of digits are also to be specified in the last parameter.
DefStr$( "", "Item Cost: £", 123.3230, 2 ) // returns "Item Cost: £123.32"
DefStr$( a$, "Item Cost: £", 123.3230, 2 ) // returns "Item Cost: £123.32"
DefStr$( "Bag Cost: £", "Item Cost: £", 123.3230, 2 ) // outputs "Bag Cost: £123.32"
iEntityCount = 2 : DefStr$( a$, "Unknown entity count: ", iEntityCount, 0 ) // returns "Unknown entity count: 2"
type$ = "Box size: " : DefStr$( type$, "Unknown entity count: ", iEntityCount, 1 ) // returns "Box size: 2.0"
Without needing to join numerous Str$(), number to string conversions using the plus sign, you need only write one function call with the number of parameters you have. Str2$() will join 2 numbers with 2 strings, Str3$() will do the same with three pairs; and DecStr3$() is the equivalent which will also place a certain amount of decimal strings on each number.
Str3$( "Location X: ", 1, " | Y: ", 10, " | Z: ", 0 ) // returns "Location X: 1 | Y: 10 | Z: 0"
DecStr3$( "Location X: ", 1, " | Y: ", 10, " | Z: ", 0, 2 ) // returns "Location X: 1.00 | Y: 10.00 | Z: 0.00"
When a number represents a change in form, quantity or behaviour; a + or - sign can be used to reflect the change, when numbers below zero indicate a reduction, and vise verse, numbers above zero indicate addition.
PosNegStr$( "Bonus: ", 12.013, 2) // returns "Bonus: +12.01"
PosNegStr$( "Bonus: ", -22.23, 1) // returns "Bonus: -12.2"
PosNegStr$( "Bonus: ", 0, 1) // returns "Value bonus: 0.0"
//////////////////////////////////////////////////////////////////////////
` Return sText$ only if it is not NULL, otherwise return sDefault$
Function Def$( sText$, sDefault$ )
If sText$ = "" Then ExitFunction sDefault$
EndFunction sText$
//////////////////////////////////////////////////////////////////////////
` Return sPrefix$ followed by fValue# with iDecimals amount of decimal places as a string
` only if sPrefix$ is not NULL. Otherwise replace sPrefix$ with sDefaultPrefix$
Function DefStr$( sPrefix$, sDefaultPrefix$, fValue#, iDecimals )
If sPrefix$ = ""
sText$ = sDefaultPrefix$ + Str$( fValue#, iDecimals )
Else
sText$ = sPrefix$ + Str$( fValue#, iDecimals )
EndIf
EndFunction sText$
//////////////////////////////////////////////////////////////////////////
` Return sText$ followed by fValue# with iDecimals amount of decimal places as a string.
` If fValue# is negative, prefix it with a -, otherwise prefix it with a + if it is positive.
` Insert no prefix if it is zero.
Function PosNegStr$( sText$, fValue#, iDecimals )
Local sSuffix$ : Local sReturn$
If fValue# > 0
sSuffix$ = "+"
Else
If fValue# < 0
sSuffix$ = "-"
EndIf
EndIf
sReturn$ = sText$ + sSuffix$ + Str$( fValue#, iDecimals )
EndFunction sReturn$
//////////////////////////////////////////////////////////////////////////
` The following value to string functions prefix each value with sText$
Function Str1$(sText$, fValue#)
sResult$ = sText$ + Str$(fValue#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function Str2$(sText$, fValue#, sText2$, fValue2#)
sResult$ = sText$ + Str$(fValue#) + sText2$ + Str$(fValue2#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function Str3$(sText$, fValue#,sText2$, fValue2#,sText3$, fValue3#)
sResult$ = sText$ + Str$(fValue#) + sText2$ + Str$(fValue2#) + sText3$ + Str$(fValue3#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function Str4$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#)
sResult$ = sText$ + Str$(fValue#) + sText2$ + Str$(fValue2#) + sText3$ + Str$(fValue3#) + sText4$ + Str$(fValue4#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function Str5$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#, sText5$, fValue5#)
sResult$ = sText$ + Str$(fValue#) + sText2$ + Str$(fValue2#) + sText3$ + Str$(fValue3#) + sText4$ + Str$(fValue4#) + sText5$ + Str$(fValue5#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
` The following value to string functions prefix each value with sText$.
` Values are return with iDecimals amount of decimal places
Function DecStr1$(sText$, fValue#, iDecimals)
sResult$ = sText$ + Str$(fValue#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecStr2$(sText$, fValue#, sText2$, fValue2#, iDecimals)
sResult$ = sText$ + Str$(fValue#, iDecimals) + sText2$ + Str$(fValue2#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecStr3$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, iDecimals)
sResult$ = sText$ + Str$(fValue#, iDecimals) + sText2$ + Str$(fValue2#, iDecimals) + sText3$ + Str$(fValue3#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecStr4$(sText$, fValue#,sText2$, fValue2#,sText3$, fValue3#, sText4$, fValue4#, iDecimals)
sResult$ = sText$ + Str$(fValue#, iDecimals) + sText2$ + Str$(fValue2#, iDecimals) + sText3$ + Str$(fValue3#, iDecimals) + sText4$ + Str$(fValue4#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecStr5$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#, sText5$, fValue5#, iDecimals)
sResult$ = sText$ + Str$(fValue#, iDecimals) + sText2$ + Str$(fValue2#, iDecimals) + sText3$ + Str$(fValue3#, iDecimals) + sText4$ + Str$(fValue4#, iDecimals) + sText5$ + Str$(fValue5#, iDecimals)
EndFunction sResult$
If desired modify the following to work with your own message box functions
//==================================================================
Function MStr1(string$, val#)
sResult$ = string$ + " " + Str$(val#)
message( sResult$ )
EndFunction val#
//==================================================================
Function MStr2(string$, val#, string2$, fValue2#)
sResult$ = string$ + " " + Str$(val#) + " " + string2$ + " " + Str$(fValue2#)
Message( sResult$ )
EndFunction
//==================================================================
Function MStr3(string$, val#,string2$, fValue2#,string3$, fValue3#)
sResult$ = string$ + " " + Str$(val#) + " " + string2$ + " " + Str$(fValue2#) + " " + string3$ + " " + Str$(fValue3#)
Message( sResult$ )
EndFunction
//==================================================================
Function MStr4(string$, val#,string2$, fValue2#,string3$, fValue3#, string4$, fValue4# )
sResult$ = string$ + " " + Str$(val#) + " " + string2$ + " " + Str$(fValue2#) + " " + string3$ + " " + Str$(fValue3#) + string4$ + " " + Str$(fValue4#)
Message( sResult$ )
EndFunction
Add spaces inbetween formatted value strings using the following:
//////////////////////////////////////////////////////////////////////////
` The following value to string functions prefix each value with sText$
Function SpacedStr1$(sText$, fValue#)
sResult$ = sText$ + " " + Str$(fValue#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function SpacedStr2$(sText$, fValue#, sText2$, fValue2#)
sResult$ = sText$ + " " + Str$(fValue#) + " " + sText2$ + " " + Str$(fValue2#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function SpacedStr3$(sText$, fValue#,sText2$, fValue2#,sText3$, fValue3#)
sResult$ = sText$ + " " + Str$(fValue#) + " " + sText2$ + " " + Str$(fValue2#) + " " + sText3$ + " " + Str$(fValue3#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function SpacedStr4$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#)
sResult$ = sText$ + " " + Str$(fValue#) + " " + sText2$ + " " + Str$(fValue2#) + " " + sText3$ + " " + Str$(fValue3#) + " " + sText4$ + " " + Str$(fValue4#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function SpacedStr5$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#, sText5$, fValue5#)
sResult$ = sText$ + " " + Str$(fValue#) + " " + sText2$ + " " + Str$(fValue2#) + " " + sText3$ + " " + Str$(fValue3#) + " " + sText4$ + " " + Str$(fValue4#) + " " + sText5$ + " " + Str$(fValue5#)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
` The following value to string functions prefix each value with sText$.
` Values are return with iDecimals amount of decimal places
Function DecSpacedStr1$(sText$, fValue#, iDecimals)
sResult$ = sText$ + " " + Str$(fValue#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecSpacedStr2$(sText$, fValue#, sText2$, fValue2#, iDecimals)
sResult$ = sText$ + " " + Str$(fValue#, iDecimals) + " " + sText2$ + " " + Str$(fValue2#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecSpacedStr3$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, iDecimals)
sResult$ = sText$ + " " + Str$(fValue#, iDecimals) + " " + sText2$ + " " + Str$(fValue2#, iDecimals) + " " + sText3$ + " " + Str$(fValue3#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecSpacedStr4$(sText$, fValue#,sText2$, fValue2#,sText3$, fValue3#, sText4$, fValue4#, iDecimals)
sResult$ = sText$ + " " + Str$(fValue#, iDecimals) + " " + sText2$ + " " + Str$(fValue2#, iDecimals) + " " + sText3$ + " " + Str$(fValue3#, iDecimals) + " " + sText4$ + " " + Str$(fValue4#, iDecimals)
EndFunction sResult$
//////////////////////////////////////////////////////////////////////////
Function DecSpacedStr5$(sText$, fValue#, sText2$, fValue2#, sText3$, fValue3#, sText4$, fValue4#, sText5$, fValue5#, iDecimals)
sResult$ = sText$ + " " + Str$(fValue#, iDecimals) + " " + sText2$ + " " + Str$(fValue2#, iDecimals) + " " + sText3$ + " " + Str$(fValue3#, iDecimals) + " " + sText4$ + " " + Str$(fValue4#, iDecimals) + " " + sText5$ + " " + Str$(fValue5#, iDecimals)
EndFunction sResult$
More snippets