Search code examples
asp.netvb.netclassgetter-setter

How can I get my variable to change the value?


I need to set this variables click to a different number. However when I reuse ServicesForm(), click is always 0 even after adding 1. How can I set a variable for the entire class?

Getter/Setter:

Class ServicesVariables

    Private click As Integer = 0

    Public Property clicks() As Integer
        Get
            Return click
        End Get
        Set(ByVal value As Integer)
            click = value
        End Set
    End Property

End Class

Trying to use it here (removed most of my code to just show you the main part):

Partial Class openapps_myapp Inherits System.Web.UI.Page

 Private Sub ServicesForm()
        Dim e As ServicesVariables = New ServicesVariables()
        e.clicks += 1
 End Sub

End Class

Solution

  • Each time you call ServicesForm(), you create a New instance of the ServicesVariables class, which means that the value of the clicks property only gets changed once per instance.

    If you want it to be reusable, you need to change the scope of your ServicesVariables object by declaring it at the class level:

    Partial Class openapps_myapp Inherits System.Web.UI.Page
    
        Private MyServicesVariables As ServicesVariables = New ServicesVariables()
    
        Private Sub ServicesForm()
            MyServicesVariables.clicks += 1
        End Sub
    End Class
    

    Additional notes:

    • I changed the variable name because it's not recommended to use one-letter-variables specially at class level. But you can still use any other variable name if you want.

    • You shouldn't use the same name for the property and its backing private field. You can name your property Clicks and the private field _clicks, for example. Actually, this is a good convention to follow...

    • ... That said, unless you're planning to change the logic of the clicks property, you can get rid of the private variable and you can shorten your property like the following:

      Public Property Clicks As Integer
      

      That's it! This is really all you need in this case which is called an Auto-Implemented Property. The compiler creates a hidden backing field for you and takes care of the implementation of the property.


    Back to the original issue, there's actually another way to have a property that its value can be changed from anywhere, which is to create a Shared Property. Shared Properties don't need an instance of the class in order to use them, they are shared across all instances.

    You should only use a shared member when it doesn't belong to an instance of the object, in other words, its value isn't different for each instance. If that's what you initially wanted, you can have your ServicesVariables class look like this:

    Public Class ServicesVariables
    
        Public Shared Property Clicks As Integer
    
        ' ..
        ' ..
    
    End Class
    

    And, in your ServicesForm() method, you can access the Clicks property directly without creating an instance of its class:

    Private Sub ServicesForm()
        ServicesVariables.clicks += 1
    End Sub