locked
declaring variables in a class RRS feed

  • Question

  • User-1820614410 posted

    What's the proper way to declare variables in a class? I've been doing something like:

    'in a class
    public shared teststring As String = "first"
    
    'on a code behind
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      response.write("Current: " & someclass.teststring & "<br/>")
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
      someclass.teststring = "hello world"
      response.write(someclass.teststring)
    End Sub


    with shared variables loading a second window or reloading the page (without clicking the button) renders the hello world string. so how do I declare variables in a class but make it per instance? Thanks

    Friday, March 5, 2010 5:15 PM

Answers

  • User-573138384 posted

    For that you need to create an instance of the object and access the variable from that instance.

    Lets assume your class name is TestClass like shown below then

    Public Class TestClass
        Public teststring As String = "first"
    End Class

    Create instance of that class and access public members like

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim someclass As New TestClass()
            Response.Write("Current: " & someclass.teststring & "<br/>")
        End Sub
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim someclass As New TestClass()
            someclass.teststring = "hello world"
            Response.Write(someclass.teststring)
        End Sub


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 6, 2010 1:54 AM

All replies

  • User-68639941 posted

    hi,

     you have declared the variable as shared i.e

     only one copy of that variable is shared across the class objects, so whoever requesting this page all will share this variable, that means it will persist the changes of this variable across the request or users.

    if you want to maintain a separate copy for each request then you can remove the "shared" keyword

    hi , while accessing dont use ClassName i.e simple access teststring instead of someclass.teststring

    Friday, March 5, 2010 11:12 PM
  • User-1820614410 posted

    yes I understand that but see:


    so how do I declare variables in a class but make it per instance? Thanks


    if I remove the shared keyword and try to access it from the page I get an error message

    Saturday, March 6, 2010 12:44 AM
  • User-1184423958 posted

    For this scenario, you must implement Singleton pattern. Singleton pattern ensures a class has only one instance and provide a global point of access to it.

    public class MySingletonClass
        {
            #region constructor
            private static MySingletonClass instance = null;
    
            /// <summary>
            /// Gets the instance.
            /// </summary>
            /// <value>The instance.</value>
            public static MySingletonClass Instance
            {
                get
                {
                    if (instance == null)
                        instance = new MySingletonClass();
    
                    return instance;
                }
            }
    
            //Private constructor prevents the class from being created.
            private MySingletonClass()
            {
            }
            #endregion
    
    	
    	public teststring As String
    }


    Then, access your variable like this...

    MySingletonClass.Instance.teststring = "Saravanan M"


     

     

    Saturday, March 6, 2010 1:07 AM
  • User-573138384 posted

    For that you need to create an instance of the object and access the variable from that instance.

    Lets assume your class name is TestClass like shown below then

    Public Class TestClass
        Public teststring As String = "first"
    End Class

    Create instance of that class and access public members like

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim someclass As New TestClass()
            Response.Write("Current: " & someclass.teststring & "<br/>")
        End Sub
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim someclass As New TestClass()
            someclass.teststring = "hello world"
            Response.Write(someclass.teststring)
        End Sub


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 6, 2010 1:54 AM
  • User-1820614410 posted

    worked. thanks!

    Monday, March 8, 2010 9:20 AM