tags:

views:

56

answers:

2

In VBA, I want to use a name for a local variable that I'd also like to use for a function name. The problem I'd that the function name formatting always changes to the local variable formatting.

Any way to prevent that?

+2  A: 

I highly recommend not using the same name for disambiguation purposes. Also, if VBA is not case sensitive, it may not know whether you are referring to the function or the variable and thus give a runtime error (I don't think it is compiled per se, but it goes to a proprietary p-code intermediate.)

Often when you'd like the names to be similar, it can be useful to prepend an underscore to one, such as a local variable. Thus I recommend you name the function FunctionName and the variable _FunctionName if you want to go that route.

If you want to try having the same name for each, you will likely need to edit the code outside of the IDE that is reformatting your code. In an editor that doesn't try to auto-format, you may be able to force it. Then whether it compiles or not is the question.

JYelton
I'm trying to stop using Hungarian but this isn't making things easy. Also, VBA doesn't allow variables to start with an underscore.
Cdl56
Okay: function_name and function_name_var? Also, why stop using Hungarian? (Always looking for reasoning on that.)
JYelton
Because people say it's bad and they'll make fun of me and take my lunch money.
Cdl56
Wikipedia (http://en.wikipedia.org/wiki/Hungarian_notation) offers some interesting pros and cons about Hungarian. Personally, this comes down to chocolate or vanilla, for me. It's preference.
JYelton
I suppose we should clarify that Systems Hungarian notation is when you include the type of variable in its name, like strName which means it is a string Name. This is the one that is often frowned upon. The other, Apps Hungarian, is when the purpose is included, as a hint to yourself, like numHorses, which indicates it is the number of horses not color or something else. Subtle but important difference. In either case, both conventions utilize a form of CamelCasing. No matter what combination of naming one uses, however, I still maintain functions and variables never be named identically.
JYelton
+1  A: 

In Visual Basic, each function already has a variable named after the function. Assigning a value to this variable is the only way to set a return value for this function.

Therefore, declaring yet another variable of the same name creates ambiguity. You cannot do so. It results in the Duplicate declaration in current scope error.

And if by "local" variable you meant a module-level variable, then you cannot do it either: Ambiguous name detected: %s.

And if you are asking about a situation when a function and a variable of the same name belong to different scopes, then VBA will use the case of the line that was edited last. So if you declare a function and then declare a variable in another module, the function name will change case. But if you then return to the function, change its casing back and press Enter, the variable, in its turn, will change its casing to match the function name.

GSerg