In the standard IDE for VBA, intellisense is built-in to many standard VBA functions. i.e. the buttons variable for msgbox() gives you a list of options for how you want the messagebox to be displayed. This way, the developer doesn't have to memorize or look up the options every time function is used.
Can I achieve the same for my custom VBA functions? This is a rough example, but can i write something like:
Public Function DoSomething(X as string)(Options X="Opt1","Opt2") as variant
...
When I call this function, I would get a pop-up giving my options for X as Opt1 and Opt2
You'll need to declare your own enumerations, and then define the parameter to your functions as that enumerated type.
Public Enum eOptions
Option1
Option2
End Enum
public Function DoSomething(ByVal x as string, Byval MyOption as eOptions)
When you call the function ala this:
Call DoSomething("myValue", Option2)
You'll see the values available for the second parameter to the function as either "Option1" or "Option2".
Related
I have a function I want to add to dynamically as the program runs.
Let's say I have function Foo:
function foo()
Function1()
Function2()
Function3()
end
and I want to change Foo() to:
function foo()
Function1()
Function2()
Function3()
Function4()
end
later in the program. Is there any way to do this?
Just do it. The code that you wrote works fine. Functions in Lua can be redefined as desired.
If you don't know what foo does, you can do this:
do
local old = foo
foo = function () old() Function4() end
end
Or perhaps it is clearer to use a table of functions:
local F={ Function1, Function2, Function3 }
function foo()
for i=1,#F do F[i]() end
end
Latter, do
F[#F+1]=Function4
and you don't need to redefine foo.
This is a supplementary answer with background information.
Lua identifiers are used for global variables, local variables, parameters and table fields. They hold any type of value.
Lua functions are values. Lua functions are all anonymous, regardless of the syntax used to define them.
function f()
--...
end
is a Lua statement that compiles to a function definition and an assignment to a variable. It's an alternate to
f = function()
--...
end
Each time a function definition is executed, it produces a new function value, which is then used in any associated expression or assignment.
It should be clear that neither statement necessarily creates a new variable nor requires it to always have the same value, nor requires it to always hold a function value. Also, the function value created need not always be held only by the one variable.
It can be copied just like any other value.
Also, just like other values, function values are garbage collected. So, if f had a function value and is assigned a different value or goes out of scope (say, if it wasn't a global variable), the previous value will be garbage collected when nothing else refers to it.
Without any other context for function f() end, we would assume that f is a global variable. But that's not necessarily the case. If f was an in-scope local or parameter, that is the f that would be assigned to.
I'm working on a visual basic 6 and we have product made of VB6 modules that use each other. Every module has it's own exe.
I'm having a problem when I'm referring to one function in one module, which works, and in another module it doesn't.
For instance, in one module I'm calling the original VB6 Round function which takes following params:
Round(number,0)
But in another module there's a function defined as
Function Round(ByVal X As Variant) As Variant
That should be called as
Round(number)
And that causes a compile time error and it says that function call has a wrong number of parameters, while on other modules where this function is undefined there are no errors.
Now, I could use it, but there are other places where I actually need to specify decimal point precision where I call it as
Round(number,2)
Round(number,3)
etc.
How do I disambiguate between these functions to call only and ONLY the original VB6 rounding function?
I would recommend to avoid such ambiguities by choosing better names for your methods. If you can´t change the method name you can use the full qualified name of the function.
VBA.Math.Round number, 2
What is the difference between function and procedure ?Aprart from returning value
Because function can also be used as a procedure if you dont return any value then what is the difference...then what is the use of functions ?
Please specify a scenario where we can use functions and procedures ??
Since your title specifies VBA and VB6, I'll reference the type of subroutines used by those languages. VBA and VB6 use "Function" for subroutines that return a value and "Sub" for those that do not. It's certainly possible to use a Function for all of your subroutines and just ignore the return value. Unlike C++ and many other languages, you're not required to return a value from a VB function. Because VB automatically initializes all variables to default values (zero for number types, False for Boolean, the empty string for String, etc), any functions that don't explicitly return a value will simply return their default value, which you can ignore. For example:
Function MyFunc() As Long
' Nothing here
End Function
This function will return the value zero.
So while you can use Function in place of Sub and just ignore the return values, it's not a good programming practice. Other users of your code will assume you chose Function instead of Sub because you intended to return something meaningful and will likely be surprised to discover that you're not returning anything at all!
There MAY also be a slight performance hit when using Function vs Sub, due to the extra parameter value that is passed on the calling stack (the return value).
Action script is developed based on object oriented programming but why does it not support function overloading?
Does Flex support overloading?
If yes, please explain briefly with a real example.
As you say, function overloading is not supported in Action Script (and therefore not even in Flex).
But the functions may have default parameters like here:
public function DoSomething(a:String='', b:SomeObject=null, c:Number=0):void
DoSomething can be called in 4 different ways:
DoSomething()
DoSomething('aString')
DoSomething('aString', anObject)
DoSomething('aString', anObject, 123)
This behavior maybe is because Action Script follows the ECMA Script standard. A function is indeed one property of the object, so, like you CAN'T have two properties with the same name, you CAN'T have two functions with the same name. (This is just a hypothesis)
Here is the Standard ECMA-262 (ECMAScript Language Specification) in section 13 (page 83 of the PDF file) says that when you declare a function like
function Identifier(arg0, arg1) {
// body
}
Create a property of the current variable object with name Identifier and value equals to a Function object created like this:
new Function(arg0, arg1, body)
So, that's why you can't overload a function, because you can't have more than one property of the current variable object with the same name
It's worth noting that function overloading is not an OOP idiom, it's a language convention. OOP languages often have overloading support, but it's not necessary.
As lk notes, you can approximate it with the structure he shows. Alternately, you can do this:
public function overloaded(mandatory1: Type, mandatory2: Type, ...rest): *;
This function will require the first two arguments and then pass the rest in as an array, which you can then handle as needed. This is probably the more flexible approach.
There is another way - function with any parameters returns anything.
public function doSomething(...args):*{
if(args.length==1){
if(args[0] is String){
return args[0] as String;
}
if(args[0] is Number){
return args[0] as Number;
}
}
if(args.length==2){
if(args[0] is Number && args[1] is Number){
return args[0]+args[1];
}
}
}
You can't overload, but you can set default values for arguments which is practically the same thing, but it does force you to plan your methods ahead sometimes.
The reason it doesn't is probably mostly a time/return on investment issue for Adobe in designing and writing the language.
Likely because Actionscript looks up functions by function name at runtime, rather than storing them by name and parameters at compile time.
This feature makes it easy to add and remove functions from dynamic objects, and the ability to get and call functions by name using object['functionName'](), but I imagine that it makes implementing overloading very difficult without making a mess of those features.
I have a form module in my access project. In the top of the module, I declare a variable like so:
option explicit
private id_foo as long
I want to explicitely state that I need the variable in my form module by using the private access modifier on it.
Now, further down in the same form module, I have a function that needs to know and/or modify the value of id_foo:
function bar() as long
call do_something(me.id_foo)
end function
Yet, this doesn't work. But when I change the private modifier to a public modifer like
public id_foo as long
it does work.
This behaviour strikes me as odd or unintuitive, and, in fact, I can't see the meaning of public and private if I have to declare the variable as public anyway in order to use it in the same form module.
So, am I overlooking something obvious or is this how it is supposed to be?
Thanks / Rene
Try it without the "me" in front of id_foo:
function bar() as long
call do_something(id_foo)
end function
If you use the me keyword, you can see only public members, properties (also Form and VBA).
A form along with its module actually represents a class object. You can also create (instanciate) multiple Instances of that class object.
So, Any variable you declare as public becomes a public property of that class object. Note that any function in the forms code module declared as public becomes a public method of that class object. All of these properties and methods then show up in the intel-sense when you type in the "me." keyword.
If you declare the variable as private, then that variable (or function) will not be exposed as a public property (variable) or a public method (function) of the form.
So the simple solution your cases is to drop the use of the me keyword in your code, and it you code will run just fine at.
So declaring as public or private does have an effect here. In fact, “private” is the default.
So, public will expose the variable and/or functions as properties and methods of that form which is a class object (note that you can have Multiple instances of the same form loaded at the same time).
If you decleare things as private (the default, so you don’t have to do anything for the Variable or function) then you can still use the value in ANY code routine in that forms code module, but it will not be public exposed as a property/method and thus you can't use me.
Thus, your code will work fine if you remove the use of the me., and just go:
function bar() as long
call do_something(id_foo)
end function