0

I have

Dim PackageInnerHorizontalwidth As Single

In "Public Sub PackageDimensionsCalculator()" which is a result of some calculations. Now I need to use that resulting value "PackageInnerHorizontalwidth" in Module "Function EvaluateFormatSketch() As Boolean" And it returns that value as "0" which is not the actual resulting value after all teh calculations. I tried to make that first

Dim PackageInnerHorizontalwidth As Single

Global and public but with no success. I understand that it's not the way to do that in my case.

So my question is: How to get "PackageInnerHorizontalwidth" as value in module "Function EvaluateFormatSketch() As Boolean"?

Right now I have it as

Dim PackageInnerHorizontalwidth As Integer

Would someone , please, help me on this one?

3

2 Answers 2

5

Used inside a procedure scope, the Dim keyword declares a local variable.

Inside a procedure scope, you can only declare local variables; local variables live & die within the scope they're declared in.

If you need to access a variable from more than one single scope, you have several options:

  • Promote the local variable to global scope by moving its declaration into a module's declarations section, changing the keyword to Public: the variable is now accessible from all procedure scopes in all modules. That's usually a rather bad idea.
  • Promote the local variable to module level (move its declaration into the module's declarations section), either keeping the Dim keyword or changing it to Private: the variable is now accessible from all procedure scopes in that module. Likely also a bad idea.

Note that Public and Private keywords (and the deprecated Global keyword too) are illegal in local scope.

There are other options, but before we look into them let's re-look at this statement:

In "Public Sub PackageDimensionsCalculator()" which is a result of some calculations.

A Sub procedure does not have a "result". Sub procedures do something, they don't return any results. We can make it actually return a result two ways:

  • ByRef return: we could declare a ByRef parameter, and use it to "return" the result:

    Public Sub PackageDimensionsCalculator(ByRef outResult As Single)
        '...do the calculations...
        outResult = ... ' <~ assign the return value before procedure exits!
    End Sub
    
  • Change it to a Function. Function procedures have one purpose: take inputs, compute a result, and return it:

    Public Function PackageDimensionsCalculator() As Single
        '...do the calculations...
        PackageDimensionsCalculator = ... ' <~ assign the return value before procedure exits!
    End Sub
    

Now EvaluateFormatSketch() can get the value by invoking the function:

Public Function EvaluateFormatSketch() As Boolean
    Dim pckgDimensions As Single
    pckgDimensions = PackageDimensionsCalculator

    'return a Boolean value by assigning to the function identifier:
    EvaluateFormatSketch = pckgDimensions > 0
End Function

Or with a ByRef return:

Public Function EvaluateFormatSketch() As Boolean
    Dim pckgDimensions As Single
    PackageDimensionsCalculator outResult:=pckgDimensions

    'return a Boolean value by assigning to the function identifier:
    EvaluateFormatSketch = pckgDimensions > 0
End Function

Consider renaming PackageDimensionsCalculator so that its name begins with a verb, e.g. CalculatePackageDimensions, noting that it's suspicious to have a procedure that calculates something but doesn't take any inputs. Consider refactoring any inputs as parameters, making the caller responsible for getting the values that affect the calculation - the function will be much easier to test/validate (and eventually, to maintain/debug) if all its inputs & dependencies are passed as parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

"That's usually a rather bad idea." - needs bolded, underlined, highlighted, whatever emphasis can be added hahah.
1

Global and public but with no success. I understand that it's not the way to do that in my case.

But it is. Put:

Public PackageInnerHorizontalwidth As Single

in the top of the module (outside/before your function) and remove any dimming of it in the functions.

2 Comments

I can't do that because it's a result of calculations prior in the sub. It returns "Invalid attribute in sub or function" error. And If I place that before "Function EvaluateFormatSketch() As Boolean" returns it as "0" which is not it's actual value.
@Eduards - skip the public variables and use a Function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.