|
-
Jun 18th, 2008, 03:42 PM
#1
Thread Starter
Hyperactive Member
Universal Variable
Can I create a variable in VB that I can access in any form or module? I just want to know if that's possible and how to do it. I don't want any alternatives. Thank you
-
Jun 18th, 2008, 03:48 PM
#2
New Member
Re: Universal Variable
I know one way is to declare a Global variable outside of a function or a sub.
I think this can only be done inside a module, but I could be wrong on this
Code:
Global MyVar as String
This will then work across the entire project.
EDIT - Make sure that you declare this at the very beginning of a module, ie, before you have begun a Sub or Function.
-
Jun 18th, 2008, 04:11 PM
#3
Re: Universal Variable
I think it's usual to use the Public keyword nowadays rather than the Global keyword.
You can use a Public variable in a form but you have to specify the form name when you wish to reference it outside of the form... Form1.MyPublicString. I believe this is not recommended because a Form unlike a Module is effectively a Class which can be unloaded and dereferenced.
-
Jun 18th, 2008, 04:21 PM
#4
Re: Universal Variable
 Originally Posted by Milk
You can use a Public variable in a form but you have to specify the form name when you wish to reference it outside of the form... Form1.MyPublicString. I believe this is not recommended because a Form unlike a Module is effectively a Class which can be unloaded and dereferenced.
When talking about a public variable inside a form, you're really talking about adding a public property to a form:
vb Code:
Option Explicit
Private mstrTest As String
Private Sub Form_Load()
mstrTest = "Hello, World!"
End Sub
Public Property Let Test(pstrTest As String)
mstrTest = pstrTest
End Sub
Public Property Get Test() As String
Test = mstrTest
End Sub
Debug.Print Form1.Test
-
Jun 18th, 2008, 05:52 PM
#5
Re: Universal Variable
Creating as a property is a better idea, as it will be a property anyway. So by declaring Public Test As String you're actually doing the same Ellis Dee posted above, just with a shorter syntax, but if you want to have any handling you have to do the long syntax anyway.
As for creating public properties such as that one, they're useful in some cases even in forms. Say you have made a form of which there can be multiple copies running, but you want to control certain aspect individually, such as you've added a gradient background feature and thus want to add properties BackColor2 (OLE_COLOR) and Gradient (Boolean) to have control over the new feature.
However, keeping public information such as program settings is better to do in a module or centralize it into it's own class module. This goes as far as into the application design: forms as visual and interactive elements should be dedicated for that, and shoudn't be used as the main storage of the information.
-
Jun 18th, 2008, 05:55 PM
#6
Re: Universal Variable
On top of any module (that is not form or class), declare:
Code:
Public myUniversalVariable As AnyType
-
Jun 18th, 2008, 06:47 PM
#7
Re: Universal Variable
Public has replaced Global
Private has replaced Dim
All work but Public and Private are now perfered over Global and Dim
-
Jun 18th, 2008, 07:14 PM
#8
Re: Universal Variable
 Originally Posted by jmsrickland
Public has replaced Global
Private has replaced Dim
All work but Public and Private are now perfered over Global and Dim
Public has replaced Global: OK!
Private has replaced Dim: Not quite right!
You cannot replace
Code:
Sub ABC()
Dim x As Long
...
End Sub
with
Code:
Sub ABC()
Private x As Long
...
End Sub
Private can only be used instead of Dim at Module level.
-
Jun 18th, 2008, 07:24 PM
#9
Re: Universal Variable
Well, that's what we were talking about; a universal variable as the OP put it.
-
Jun 18th, 2008, 07:38 PM
#10
Thread Starter
Hyperactive Member
Re: Universal Variable
Okay, thank you! So, what's the difference between using "Global" or "Public" in a module? Also, I was wondering, is "Dim" short for something?
-
Jun 18th, 2008, 07:50 PM
#11
Re: Universal Variable
Along with "Public has replaced Global", the statement "Private has replaced Dim" is misleading.
"Public" is the new keyword for "Global" (both can be used interchangeable), they can be used to declare a variable on top of a module or a procedure (Sub/Function), these variable and procedure can be accessed anywhere in the project (unless there is other restriction).
"Private" and "Dim" can be used interchangeable at Module level only, and "Private has not replaced Dim".
"Private" is also can be used to declare a procedure that will be available within the module only.
Last edited by anhn; Jun 18th, 2008 at 07:54 PM.
-
Jun 18th, 2008, 09:45 PM
#12
Re: Universal Variable
"Dim" is short for "Dimension"
-
Jun 18th, 2008, 10:33 PM
#13
Re: Universal Variable
"Dim" is "Dim", it means dimension but it is not the short form of "Dimension", ie. "Dimension" cannot be used in places of "Dim".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|