0

I'm working on a form in a VB6 application. I have a question if anyone can help.

if i create a function like this...

 Public Function CheckUser(userID as integer) as boolean
 CheckUser = False
 'do stuff here 

 CheckUser = True
 Exit Function

I'm calling this function from another procedure (same form) - can i use the Checkuser boolean value throughout the form, or is it only for this procedure? Basically I'm trying to return a boolean value back to my procedure after running this function - and don't really want to declare a new modular variable and go that way. Trying to keep it neat.

cyboashu
  • 10,196
  • 2
  • 27
  • 46
BobSki
  • 1,531
  • 2
  • 25
  • 61
  • 1
    Yes. As long as you are calling it in the same form.It will work as it is. – cyboashu Aug 12 '16 at 17:46
  • I suspect that isn't what you are asking for. No, it does not create some magical CheckUser variable in hyperspace. As you have it written you must call it each time you want the value. This smells strongly of a VBA question and not something a VB6 programmer would even ask. – Bob77 Aug 12 '16 at 17:53
  • @Bob77 Just asking. no need to be rude. – BobSki Aug 12 '16 at 18:24
  • If `CheckUser` is always going to be true why even have it as a function (you already know it's going to be true)? Make it a method and call it to perform what you would need... – Trevor Aug 12 '16 at 18:25
  • its not always going to be true. i set it to false, if it passes all my test then it goes to true, else i do exit sub. question is answered. Thank you for your help! – BobSki Aug 12 '16 at 18:41
  • @drunken code monkey i agree a 100% . I use both vb6 and vba on daily basis, for the most part theyre identical. – BobSki Aug 12 '16 at 23:48

3 Answers3

1

yes you can if you declare CheckUser as a global, otherwise it is not possible because of local in method.. this is will help you how to declare global variable. how to declare global variable

Community
  • 1
  • 1
Ummer Iqbal
  • 136
  • 1
  • 2
  • 11
1

This is a question of scope:

https://support.microsoft.com/en-ca/kb/141693

The general rule is that a variable is only in scope for descendant members of the structure in which it was declared. If you declare a variable in a function, as is happening here through VB's implicit function variable, that variable only exists within that function.

The fact that the access modifier is public has nothing to do with it. You can read more about access modifiers here:

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18
1

Firstly. VB6 is a forms package and application object (plus a printer and clipboard object) that hosts the VBA language.

In programming there are functions (and methods and properties of objects are also functions) and variables.

In your code to keep using it you would have to go If CheckUser("12345") then each time you use it.

But function calls are slow. Parameters have to be put on the stack, local variables created on the stack, the return address put on the stack, then a call to a memory location that may not be in the CPU caches.

Therefore if using something more than once store it in a variable.

Dim GetCurrentUser As Boolean
GetCurrentUser=CheckUser("12345")

By placing the Dim at the top of a module it is available to all procedures in that module. Or by Public GetCurrentUser As Boolean for all procedures to access.

Trigger
  • 11
  • 1
  • The performance difference is negligible in comparison with the added maintenance problems using globals everywhere entice. Maintaining code is much better when you minimize "side-effect" code and keep your code self-contained. I am not saying to never use globals, but in the majority of cases function calls is much, much better for maintainability. – Drunken Code Monkey Aug 13 '16 at 04:29