Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determining if variable or static string is passed to a Sub
#1
Is there a way that I can determine from within the Sub (or Function) if a variable string was passed or a static string was passed?

Code: (Select All)

a$ = "DataThatIsPassed"

SomeRoutine (a$)
SomeRoutine (" DataThatIsPassed ")



Sub SomeRoutine (a$)
    Print "processing goes here to determine what was sent..."
    Print "If a variable was sent then VariableWasSent% = 1"
    Print "Otherwise VariableWasSent%=0"

    If VariableWasSent% = 1 Then Print "Variable string was sent"
    If VariableWasSent% = 0 Then Print "Static string was sent"
End Sub


If there exists a way to do this, the example above would provide the following results:

Variable string was sent
Static string was sent


The reason I need this is because I am adding functionality to a very old library routine that is entrenched in so many programs that is would be a monumental task to change.  In order to bring the new functionality to the old routine I need to be able to determine what was sent.
Reply
#2
SUB SomeRoutine (a$, state)
   IF state THEN PRINT "Variable" ELSE PRINT }Static"
END SUB

Then just send the sub a call with the flag desired.

SomeRoutine a$, -1
SomeRoutine "Hello World", 0

There's no auto detection, but it's simple enough to pass a flag with that information.
Reply
#3
I cannot do that since that would require changing the number of parameters to the routine.  Since this routine is in use almost everywhere I cannot change that.

Thanks for the suggestion though!
Reply
#4
In my experiments even if you Dim a string as fixed and then change it to another length, no error is thrown and the change is made. (Not something you can do with Arrays!) In fact all processing of fixed length strings seem to be treated as variable length. I guess its up to coder to keep the length fixed when resetting a value.

So I don't understand how any String processing, fixed or variable, will throw error but you might try making sure the string going out is same size as string going in? That will keep fixed strings the same length.
b = b + ...
Reply
#5
Hi Dano
Code: (Select All)

Dim GlobalString$, GlobalFixedString$7

GlobalString$ = "123456789012345"
GlobalFixedString$7 = "1234567890"
Print "Global variable", "In function variable" + "  Kind 0=fixed/ -1=variable"
Print GlobalString$, WhatKindOfString%(GlobalString$)
Print GlobalFixedString$7, , WhatKindOfString%(GlobalFixedString$7)
End

Function WhatKindOfString% (FunctionString$5)
    '-1 = Fixed lenght, 0 = variable lenght
    Print FunctionString$5, ;
    If Len(FunctionString$5) > 7 Then WhatKindOfString% = -1 Else WhatKindOfString% = 0
End Function

here screenshot that confirms affirmations of Bplus

I dunno if compiler must control this kind of data type with distinguish between fixed string and string.
In the same manner if you declare into the Function a SHARED variable, the compiler doesn't test if there is a same variable into main, but I dunno if this is a must for the compile... surely you can bypass this problem using Option _explicit at top of your code.

[Image: String-parameters-to-funciton-issue-by-Bplus.jpg]

So for now you cannot have this information directly from parameters of function!

Please say me:
I have 2 question for you:
 1. if it is difficult to add a parameter to the SUB/FUNCTION , is it difficult too to add a SHARED variable into it?
2. what is the goal to distinguish between Fixed and Variable lenght ?
Reply
#6
it would require a preprocessor.  to catch every single call to the target subprogram.  then notice if the single argument is a vls variable.  or fixed-length string.  or a literal.

this is because string technically is a pointer.  if the subprogram is called with literal string to pass to it.  the system has to make a copy of that string anyway.  to work with it inside the subprogram.  this will be expected by most people.  if a string variable is passed instead.  it could cause more trouble.  because its value could be changed inside the subprogram.  unless the programmer is willing to force the copying and extra memory spent.  work with the copy instead.  if the value going into the subprogram shouldn't be changed.

in the first post of this thread.  the variablewassent% has to be forced honesty by the programmer as smcneill pointed out.  otherwise it needs observation outside the compiler.

if this is the only subprogram that requires this special treatment.  it would be easy to write a basic program.  actually in any programming language.  that reads in the basic code.  looks only for that call of subprogram.  then make necessary adjustments by adding basic code to the output file.  then use the output file.
Reply
#7
Thanks to all.
Since SUBs and FUCTIONs are unaware, I will do what have done in the past when in the same situation - I incorporate a flag @ position 1 in the string (probably the pipe character) that alerts the routine to make the necessary adjustments.  Not perfect, but close enough and definitely workable.
Reply




Users browsing this thread: 1 Guest(s)