locked
Class Properties Not Reflecting Their Changed Values RRS feed

  • Question

  • User323840962 posted

     I am using a class to track if a user cleared their personal information found within some text boxes on a form. In the button click event to clear the text boxes I am creating a new object to the class and assigning true to one property value. Sounds simple until I try and submit the form with a different button, and create a another object reference that I use in an IF statement to check the property value for true. Every time the statement fails because the value being returned from the getter in my class is false.

    This is the class:

    Public Class FormStateClass
        '-------------------------------------------------------
        'property holds true or false 
        'based on if the user clears the populated form values.
        'True = Form was cleared
        'False = Form was not cleared
        '-------------------------------------------------------
    
        Private _test As Boolean
        Public Property FormState As Boolean
            Get
                Return _test
            End Get
            Set(ByVal value As Boolean)
                _test = value
            End Set
        End Property
    
    End Class


    This is the code I am using to set the property to true:

    Dim thisForm = New FormStateClass With {.FormState = True}


    I've also tried these statements:

    Dim thisForm As FormStateClass
            thisForm = New FormStateClass
            thisForm.FormState = True


    This is the code that I use to retrieve the property value:

     Dim obj As New FormStateClass
    
            If obj.FormState = True Then
     
    When I check the property value within obj.FormStateClass when it reaches this statement in dubug mode it's always false.
    



     

     

     

    Saturday, November 13, 2010 10:22 PM

Answers

  • User187056398 posted

    1. Dim obj As New FormStateClass   
    2.   
    3.        If obj.FormState = True Then  

     

    That is a brand new object.  It is not the same object you created earlier.  It will not have the properties of the object you created earlier.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 13, 2010 11:21 PM
  • User323840962 posted

    That is what I was figuring was the problem.

     

    Thanks for your response! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 12:11 AM
  • User323840962 posted

    I have elected to go with basically a flag variable called fmState at the begging for the form class, and for some reason the variable scope is still false when I try to use it in another sub procedure. Does anyone have any thought on why this is occurring?

    Option Explicit On
    
    Partial Class AttorneyForms_AttorneyNewCase
        Inherits System.Web.UI.Page
        Private fmState As Boolean
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 2:05 AM

All replies

  • User187056398 posted

    1. Dim obj As New FormStateClass   
    2.   
    3.        If obj.FormState = True Then  

     

    That is a brand new object.  It is not the same object you created earlier.  It will not have the properties of the object you created earlier.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 13, 2010 11:21 PM
  • User323840962 posted

    That is what I was figuring was the problem.

     

    Thanks for your response! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 12:11 AM
  • User323840962 posted

    I have elected to go with basically a flag variable called fmState at the begging for the form class, and for some reason the variable scope is still false when I try to use it in another sub procedure. Does anyone have any thought on why this is occurring?

    Option Explicit On
    
    Partial Class AttorneyForms_AttorneyNewCase
        Inherits System.Web.UI.Page
        Private fmState As Boolean
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 15, 2010 2:05 AM
  • User323840962 posted

    I fixed my problem by letting the variable declaration be private shared. 

    Monday, November 15, 2010 10:25 PM