|
-
Jun 6th, 2003, 03:22 AM
#1
Thread Starter
Lively Member
Where can I define Variables that all forms can use ?
I have some variables for authorisations that must be consulted by all forms
Where do I have to put these declaration and what is the correct syntax
for the moment I define it like that :
Public Class Form_Main
Inherits System.Windows.Forms.Form
Public Shared Key_Num_Dossier As Integer
Public Shared Key_Utilisateur As Integer
But I need to call it from other form like : Form_Main.Key_Num_Dossier
Can I define it outside a form ? and where (other vb.file?) ?
Thanks
-
Jun 6th, 2003, 03:49 AM
#2
New Member
Have these properties as shared properties in another class that you can reference from any form or other class.
Code:
Public Class Authorisations
Private Shared m_Key_Num_Dossier As Integer
Private Shared m_Key_Utilisateur As Integer
Public Shared Property Key_Num_Dossier() As Integer
Get
Return m_Key_Num_Dossier
End Get
Set(ByVal Value As Integer)
m_Key_Num_Dossier = Value
End Set
End Property
Public Shared Property Key_Utilisateur() As Integer
Get
Return m_Key_Utilisateur
End Get
Set(ByVal Value As Integer)
m_Key_Utilisateur = Value
End Set
End Property
End Class
Code:
Public Class Form1
...
Dim iKeyNumDossier = Authorisations.Key_Num_Dossier
Dim iKeyUtilisateur = Authorisations.Key_Utilisateur
...
End Class
-
Jun 6th, 2003, 05:14 AM
#3
Thread Starter
Lively Member
many thanks
What I want is to declare the variable outside Form1 and use it in all my Forms without having to create the objet in all forms.
Do you see want I mean (global variable)
Last edited by oxbow123; Jun 6th, 2003 at 05:19 AM.
-
Jun 6th, 2003, 07:29 AM
#4
Fanatic Member
Use a module to hold your global constants (shared by default) and you don't have to qualify it to use it in your forms if the module is in the same project.
-
Jun 6th, 2003, 07:42 AM
#5
New Member
That is the whole point of Shared members/properties. They act like global variables. You don't need to declare a new instance of the class every time you want to use it - you are just referencing the shared members in it.
...when you change the value of a shared field and property associated with an instance of a class, you change the value associated with all instances of the class. In this way, shared fields and properties behave like global variables that can be accessed only from instances of a class. Without static fields and properties, you would need to use module-level variables to achieve the same effect. However, module-level variables can make your classes difficult to understand and maintain. Furthermore, using module-level variables in this way violates the concept of encapsulation that classes represent.
see Microsoft Help
ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vaconSharedMembers.htm
-
Jun 6th, 2003, 07:53 AM
#6
Sleep mode
Doing it the way jaycee suggested is neater and smoother and you may need to limit writing access to these vars by using readonly keywords .
-
Jun 6th, 2003, 08:22 AM
#7
I 2nd that
Besides, I would much rather call a variable using
mystaticclass.variable
because it is easier to determine where it is defined later on when I, or someone else is looking through the code
-
Jun 6th, 2003, 10:18 AM
#8
You could use an Imports if you don't want to type the first part/class name.
-
Jun 7th, 2003, 07:14 AM
#9
Thread Starter
Lively Member
Ok you use a class to store data in private, but do I need to create an object of this class to set the values ?
dim ObjAuth as Authorisations
ObjAuth.Key_Num_Dossier = 123
Or do I just call the class directly
Authorisations.Key_Num_Dossier = 123
or
Authorisations.Key_Num_Dossier.set(123)
Thanks
-
Jun 7th, 2003, 07:32 AM
#10
Sleep mode
If the Class member are static (shared) , then you can call it without instantiating . Otherwise , you should instantiate (use New Keyword) from that class .
-
Jun 7th, 2003, 08:20 AM
#11
Thread Starter
Lively Member
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
|