Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
App. WinForms / WPF .NET
Variabili globali !!
domenica 04 settembre 2005 - 12.43
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
sasa72
Profilo
| Newbie
23
messaggi | Data Invio:
dom 4 set 2005 - 12:43
Ciao,
è da poco tempo che sto usando vb.net e non riesco a capire come posso fare per definire una variabile globale,
visibile a tutte le form.
Qualcuno può aiutarmi?
Grazie
Cteniza
Profilo
| Guru
1.509
messaggi | Data Invio:
dom 4 set 2005 - 15:25
Per le variabili globali devi definire un modulo .vb (corrispondente al vecchio moduloi di vb6 .bas) in cui immettere la tua variabile.
Public Shared myvariable As ....
Però ti sconsiglio di usare le cose in quel modo.
Normalmente per delle variabili globali si dovrebbe fare in questo modo:
Public Class Globali
Private Shared mConnessione As String = Nothing
Private Shared mDataBaseFile As String = Nothing
Private Shared mUscita As Boolean = False
Private Shared mProvider As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Private Shared mFolderIn As String
Private Shared mFolderOut As String
Private Shared mCodice As Integer = 0
Private Shared mRiga As Integer
Private Shared mCodicePrecedente As Integer = 0
Private Shared mData As DateTime = DateTime.Now
Public Shared Property Connessione() As String
Get
If mConnessione Is Nothing Then
mConnessione = mProvider + DataBaseFile + ";"
End If
Return mConnessione
End Get
Set(ByVal Value As String)
mConnessione = Value
End Set
End Property
Public Shared Property DataBaseFile() As String
Get
If mDataBaseFile Is Nothing Then
Dim s As String
Dim f As Boolean = False
For Each s In System.IO.Directory.GetFiles(Application.StartupPath, "*.mdb")
f = True
Exit For
Next
Uscita = Not f
If f = False Then
s = ""
End If
mDataBaseFile = s
End If
Uscita = Not System.IO.File.Exists(mDataBaseFile)
Return mDataBaseFile
End Get
Set(ByVal Value As String)
mDataBaseFile = Value
End Set
End Property
Public Shared Property Uscita() As Boolean
Get
Return mUscita
End Get
Set(ByVal Value As Boolean)
mUscita = Value
End Set
End Property
Public Shared Property FolderIn() As String
Get
Return mFolderIn
End Get
Set(ByVal Value As String)
mFolderIn = Value
End Set
End Property
Public Shared Property FolderOut() As String
Get
Return mFolderOut
End Get
Set(ByVal Value As String)
mFolderOut = Value
End Set
End Property
Public Shared Property Codice() As Integer
Get
Return mCodice
End Get
Set(ByVal Value As Integer)
If Value <> mCodicePrecedente Then
Riga = 0
End If
mCodice = Value
mCodicePrecedente = Value
End Set
End Property
Public Shared Property Riga() As Integer
Get
Return mRiga
End Get
Set(ByVal Value As Integer)
mRiga = Value
End Set
End Property
Public Shared Property Data() As DateTime
Get
Return mData
End Get
Set(ByVal Value As DateTime)
mData = Value
End Set
End Property
End Class
E cioè le variabili che contengono i valori non sono visibili dall'esterno della classe in quanto private, dall'esterno si vede una classe con i propri membri come proprietà e metodi (incapsulamento)
L'aggiornamento / assegnazione del valore di ciascuna proprietà può essere mediata da un controllo, cosa che invece non avviene definendo le variabili come pubbliche e basta.
E' fondamentale però che la clausola dei membri sia "Shared" perchè questa garantisce che l'oggetto "globale" possa contenere un solo valore.
sasa72
Profilo
| Newbie
23
messaggi | Data Invio:
dom 4 set 2005 - 16:12
ho provato ed ha funzionato.
Grazie mille!
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !