How to use the variable declared in one module to be used in other module function access vba - vba

Is there any way that we can use variable declared in one module and use that variable in a function defined in other module?

Yes. Declare a variable at module level like this:
Public myvar As String
It can then be accessed from other modules.
Note, however, that global variables like this are almost never a good idea.

Related

How can I use access specifiers on member variables in a function block in structured text? (Beckhoff, TwinCAT)

An important part of OOP is to use access specifiers to make member methods and variables inaccessible from outside of the object.
When declaring a function block method is is easy to control the Access Specifier, but I have not found a way to control access to member variables.
Is it possible and if yes, how?
You can actually still access internal variables of an object direcly in code (no pointers), but they are read only. The code completion will not display the internal variables though, but after you finish typing the name structure, you will see no compile errorrs - test := fb1.internalVariable will be a valid read action actually while fb1.internalVariable := 5; will end up giving you an error, saying that the variable is not an input to the function block (or any other object for that matter).
You can also use the hide oder hide_all_locals pragma to suppress local variables being found in auto-complete and crossreference-list (see https://infosys.beckhoff.com/content/1033/tc3_plc_intro/2529654667.html?id=5927203996458905204 )
Every variable that you declare under the VAR section of your Function Block is considered private.
There is no public or private keyword for variables in IEC 61131-3
Another thing you can do if you absolutely want to use public/private keywords is to define properties.
In general, the normal convention is to have read-only variables in the VAR_OUTPUT section of the Function Block and writable variables in the VAR_INPUT section of the Function Block. Again, the VAR section is considered a private section even though you could read this variables with the fbName.var notation or write them through their address (but this is a very bad programming style).
Twincat2 also allowed the variables in the VAR section to be written to with the fbName.var notation but this changed in Twincat3 in order to achieve better incapsulation.
To learn more about programming conventions in the IEC 61131-3 world, I recommend you to read the programming guidelines of the PLCOpen organization:
https://plcopen.org/guidelines/guidelines

How to declare a global variable inside a function

I am not able to find the required solution. I want to call a function and want to use some variable which is being declared in that function(I don't want to return that variable). I just want to make it global.
func foo(){
temp:=30
}
func main(){
foo()
// How to use temp without returning or without declaring it outside foo and main
}
You can't. You can only declare a package variable (aka global variable) in package scope. You can modify it from inside a function, but you can't declare it inside a function. This is because anything else referring to that variable has to have that reference resolvable at compile time. Otherwise, what happens when Bar() tries to reference the variable before Foo() could declare it? That would break compile-time safety that Go guarantees.
That said, the solution is simple; just declare it in package scope. It's unclear in the question why you wouldn't want to do that - if you want a global, that's what you do.

When using module variables as input variables, is there a way to specify intent(in) property as we do for a subroutine variable?

To remind me that a variable from a module used in a subroutine is an input rather than an output, I usually add comments to indicate this, which provides nothing to compilers.
There is no such thing in Fortran that would import a module variable as a constant. As roygvib mentioned, you can declare a variable protected inside the module to make it read only for all other modules. But you cannot import a non-protected variable as read-only in Fortran.
I recommend not to treat module variables, which are really just better global variables, as input or output. If something is clearly an input or output of your subroutine, make it an argument explicitly and call it in such a way that is clear what you are doing - with the global variable as an actual argument.

Declare variable for shared procedure inside procedure instead Imports statement

I'm using an imports statement to access shared constants and shared procedures just to shorten things a bit.
Imports vList = Helper.Stores.Departments.FruitList
But what I'd really like to do is declare it inside the procedure:
Dim vList as Helper.Stores.Departments.FruitList
Of course I get the warning: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. when I try to use it like that.
Is there a way to do something like this inside the procedure without using the imports statement?
As far as I know you can only create an alias of this kind with an import statement as you already done.
The thing that you can do inside a procedure is creating variables using the fully qualified name for the class.
Take a look at the MSDN documentation about Namespaces for more information.

What is the most proper way to declare global variable to use for many modules in Fortran?

The question is: If I want to use global variables in many modules. How should I do?
In my opinion, I think, maybe we could make another module and declare the global variables and then include it to any files that require it or something like that. I think this is a very simple way but the problem is I'm not familiar with Fortran. I don't know how to do it and how normally people do it.
Please give me some easy example.
You can just make a module, perhaps called global
module global
implicit none
real :: my_global_x
integer :: my_global_i
end module
and then you can use it wherever it is needed, in modules
module a
use global ...
end module
in subroutines
...
subroutine s
use global
...
end subroutine
...
or in the main program
program main
use global
implicit none
...
end program
You can also use just a limited number of variables from the module to avoid name-space pollution
use global, only: my_global_x