[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Question With regards to Global variables in VB.Net

Can anybody tell me if there is any better way to use global variables. I have created this new flag at global level and I am passing it to other subforms, to determine if the functionality is to be switched on:

Dim args() As Object = {Me.OrderDS, bApplicationStatusOrder}
NewOrderLineForm = Me.ActivateForm(GetType(NewOrderLine), True, args)

But I am findinding that in the Subforms I am having to add the param to alot of Subs and functions. Is there anyway I can use another structure that i can reference directly without modifying the parameter list of subs and functions.

Cheers

Anwar
0
anwarmir
Asked:
anwarmir
  • 2
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
The easiest way to handle it would be to create a class with Shared members to provide global access.

    Public Class Globals
        Public Shared FirstName As String
    End Class

Then access it with something like:

    Dim fName As String = Globals.FirstName


I only use a public variable, rather than a property, to demonstrate the idea.
0
 
Bob LearnedCommented:
Yeah, create a data access layer in a class.

Bob
0
 
anwarmirAuthor Commented:
Thanks This seems to work quite well can you tell me what a Shared members class is opposed to a standard class
0
 
Carl TawnSystems and Integration DeveloperCommented:
Shared members belong to the class itself rather than instances of the class.

So:

    Public Class Class1
        Public FirstName As String
    End Class

    Public Class Class2
        Public Shared FirstName As String
    End Class

    Dim o1 As New Class1()
    Dim o2 As New Class1()
   
    o1.FirstName = "Bob"
    o2.FirstName = "Bill"

These two objects both have a FirstName member each with a different value.

    Class2.FirstName = "Bob"
    Class2.FirstName = "Bill"

Because FirstName in Class2 is Shared both of the lines above change the same Shared member of Class2. We don not need to create an instance of Class2 in order to access or change its value.
0
 
anwarmirAuthor Commented:
Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now