1

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

4 Answers 4

2

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"
Sign up to request clarification or add additional context in comments.

6 Comments

+1, but your answer would be better if, after explaining classes, it also discouraged the use of globals at all.
Having to instantiate the class and set all of the values programmatically kind of defeats the purpose of having a class with all of your settings in it, if that's what he is trying to do. If he just wants a class with globally accessible variables, this will work.
@DouglasBarbin good software design is almost never determined by how much typing is required. Having to instantiate a class one time is hardly so difficult that it is a reason not to do it that way. Since the settings shouldn't be stored in a global variable anyway, at least if it is stored in an object, it can be passed into methods and classes that want to (properly) request the settings rather than (improperly) reaching out to a global variable to get the data. So, while it's still bad, it's at least is a step in the right direction.
@StevenDoggart I agree that it's a bad design and he shouldn't be doing it that way, but your answer doesn't accomplish what he is asking to do. His class will be public but not truly global; it will only exist in whatever scope he instantiated the object. Once he is outside of that scope or the object is disposed of, he will no longer have access to that object.
@DouglasBarbin Good point. Thomas should be more clear regarding how to declare the variable as a global.
|
2

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.

3 Comments

@douglas: There are no static classes in vb.net.
The above example works perfectly fine. Just try it out. MSDN: "Sharing a member of a class or structure makes it available to every instance"
It works fine, except that it allows the class to be instantiated, which is probably not what he wants. Somebody can instantiate the class somewhere, change the properties to something else, and since they are static (called "shared" in VB), they will overwrite what was defined in the class. That may or may not be acceptable for him. If he uses constants instead of properties, the class can still be instantiated but the constants cannot be overwritten.
1

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"

1 Comment

Yes, it is one user at a time.
1

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.