0

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

1 Answers1

1

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
  • Thank you for your input thus far! My click variable is still not storing the changed value. Do I have to tell it to set the value in a different way? – LegendOfZelda May 18 '18 at 05:37
  • Please see the updated answer and let me know what exactly what you are currently using which doesn't store the value of `clicks`. You can feel free to [edit](https://stackoverflow.com/posts/50404140/edit) your question and include your changed code if needed. – 41686d6564 stands w. Palestine May 18 '18 at 05:47
  • Glad I was able to help. Get used to the other solution too because not all problems can be solved by using shared members. – 41686d6564 stands w. Palestine May 18 '18 at 06:00