Results 1 to 11 of 11

Thread: Where can I define Variables that all forms can use ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68

    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

  2. #2
    New Member
    Join Date
    Apr 2003
    Location
    Nottingham
    Posts
    14
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68

    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.

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    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.

  5. #5
    New Member
    Join Date
    Apr 2003
    Location
    Nottingham
    Posts
    14
    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

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could use an Imports if you don't want to type the first part/class name.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68
    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

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If the Class member are static (shared) , then you can call it without instantiating . Otherwise , you should instantiate (use New Keyword) from that class .

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68
    Great it work

    Thanks to all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width