Reputation: 221
I want to use a variable that's in the scope of all my project, what's a good way to accomplish this?
public User as (some type)
(
var (sometype)
var2 (sometype)
)
Example:
If User.name = "ADMIN" Then
otherForm.Caption = User.name
otherForm.Show
End If
Upvotes: 1
Views: 2408
Reputation: 3615
If you are trying to have a "settings" class like some have suggested, you probably want to look at the My.Settings or My.Resources namespaces for VB .Net
You would end up with something like:
If User.name = My.Settings.Admin Then
otherForm.Caption = User.name
otherForm.Show
End If
Is this what you are trying to do?
Your other option is to use a module or a "Public NotInheritable" class with a private constructor, with public properties or constants. Like this:
Public NotInheritableClass ProjectSettings
Public Const Admin as String = "ADMIN"
Public Const Whatever as Decimal = 3.14D
Private Sub New()
End Sub
End Class
Then you could have:
If User.name = ProjectSettings.Admin Then
otherForm.Caption = User.name
otherForm.Show
End If
I like these solutions a little better because there is no way that you can instantiate the settings class.
If you just want your User class to be globally accessible (which implies there is only one given User at a time), then you could do something similar with the User class.
EDIT: Your User class would look like:
Public NotInheritableClass User
Public Const Name as String = "Some Name"
Public Property YouCanChangeThisProperty as String = "Change Me"
Private Sub New()
End Sub
End Class
To use it:
User.YouCanChangeThisProperty = "Changed"
MessageBox.Show("User name: " & User.Name & "; the other property is now: " & User.YouCanChangeThisProperty")
This will give you a message box with: "User name: Some Name; the other property is now: Changed"
Upvotes: 1
Reputation: 5719
You can create New Class named User
Public Class User
Private mstrName As String
Private mdBirth As Date
Public Property Name() As String
Get
Return mstrName
End Get
Set(ByVal vName As String)
mstrName = vName
End Set
End Property
Public Property BirthDate() As Date
Get
Return mdBirth
End Get
Set(ByVal vBirth As Date)
mdBirth = vBirth
End Set
End Property
ReadOnly Property Age() As Integer
Get
Return Now.Year - mdBirth.Year
End Get
End Property
End Class
You can use this class like this :
Dim Name1 as New User
Name1.Name = "ADMIN"
Name1.BirthDate = CDate("February 12, 1969")
Then Check it (by Msgbox or whatever) :
Msgbox(Name1.Name)
Msgbox(Name1.BirthDate.ToString & " and Now is " & format(Name1.Age) & " years old")
Upvotes: 1
Reputation: 10895
I suggest you define a class for such 'global' properties. For example, you could name it 'ProjectSettings'.
Public Class ProjectSettings
Public Shared CurrentUser as String
Public Shared DateTimeFormat as String
...etc...
Public Shared Sub Initialize()
'Initialize your members here
End Sub
End Class
From outside, you could access it like this:
ProjectSettings.CurrentUser
ProjectSettings.DateTimeFormat
But remember, there are heaps of different approaches of how to do this. In the above case, you could also define the Members as Readonly Properties, making sure nobody accidentally overwrites the values. Or you could define an object 'User' for CurrentUser, if you need to store more data.
It really depends on what you want to achieve with your global properties. It's only important to keep them central so that everybody in your team (including yourself) knows where to find them. Else it can easily lead to unstructured, bad code.
Upvotes: 2
Reputation: 817
You could create a class that encapsulates all of this data inside of it:
Example:
Public Class User
Public Property Name As String
Public Property Age As Integer
Sub New(_Name As String, _age As Integer)
Name = _Name
Age = _age
End Sub
End Class
Then, you'd just declare it, and set the properties:
Dim U as new User("Thomas", 18)
Messagebox.Show(U.Name) ' Will print "Thomas"
Upvotes: 2