locked
Problem with a new coded property in a custom class RRS feed

  • Question

  • Hi,

    I created a simple property and a simple function in a new custom class using VS 2005. Everything looks OK, but the variable _GetInVal  Dim in class is unable to accept the value in the SET section of the property. Run the program from a VB.NET page by add an integer value (>1) into a textbox and click OK button, it supposes to display the data returned from the Function in another textbox . An error message catched and said: "InValue can not be less then 1". I put the mouse over the value in SET section. It shows the value of what I entered in the textbox (I add 3 it shows 3, I add 4 it shows 4) , but when I put the mouse over the variable _GetInVal, it only shows 0, no matter what integer I entered to the textbox. The following is my codes Please help me to find the problem:

    Imports Microsoft.VisualBasic

    Public Class clsTry
     Private _GetInVal As Integer
      
     Public Property GetInVal() As Integer
            Get
                Return _GetInVal
            End Get
            Set(ByVal value As Integer)
                If _GetInVal > 0 Then
                    _GetInVal = value
                Else : Throw New Exception("InValue can not be less then 1")
                End If
            End Set
            End Property       

    Public Function MultiplyBySelf() As Integer

            Return Multiply(_GetInVal, _GetInVal)

        End Function
     
    End Class

    ============================================================================
    Partial Class _Default
        Inherits System.Web.UI.Page
        Private clsMath As clsTry

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            clsMath = New clsTry()
            clsMath.GetInVal = CInt(txtEnter.Text)
            txtResult.Text = clsMath.MultiplyBySelf
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub
    End Class


    zixing wang
    Monday, March 16, 2009 8:21 PM

Answers

  •   Private _GetInVal As Integer

    You created the variable but it is uninitialized so it does not equal anything . Your set method won't let it be set to anything since _GetInVal is nothing . I think Nothing is also -1 but either way your set method can not work . Shouldn't you be checking value not   _GetInVal ? value is what the method is trying to set GetInValue to shouldn't it be  

    If value > 0 Then
    _GetInVal = value

    . You can give _GetInVal a value when you create the variable as well .

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    • Proposed as answer by Heslacher Tuesday, March 17, 2009 7:02 AM
    • Marked as answer by Martin Xie - MSFT Monday, March 23, 2009 3:05 AM
    Tuesday, March 17, 2009 1:05 AM
  • Hi Heslacher,

    Well spotted!! Forum user bdbodger also spotted it, see the 1st reply in this thread.

    That is why I have proposed your post as answer.

    :-)

    I would still also do the change I suggested earlier too.
    Reason: If the user accidentally types a letter or something that is not a number into the txtEnter TextBox then

    CInt(txtEnter.Text)

    will throw an EXCEPTION error.

    Whereas this line of code will try to convert ( i.e. TryParse ) the text in the txtEnter TextBox into the INTEGER property that is clsMath.GetVal >>



    integer.tryparse(txtEnter.Text,clsMath.GetInVal)



    The result will be a zero to throw the custom exception in the SET section of the property if a zero or if anything else other than a number is typed.


    Regards,

    John

    • Proposed as answer by Heslacher Tuesday, March 17, 2009 1:21 PM
    • Edited by John Anthony Oliver Tuesday, March 17, 2009 1:57 PM Changed Heschaler to Heslacherr
    • Marked as answer by Martin Xie - MSFT Monday, March 23, 2009 3:04 AM
    Tuesday, March 17, 2009 1:10 PM
  • Hi Zixing Wang,

    it is like bdbodger says. You are checking wether _GetInVal is >0, but you never assigned any value to it. I think you wanted to check if value>0.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Tuesday, March 17, 2009 7:02 AM
  • Hi Zixing Wang,

    like i mentioned before, your problem comes from your Property

          
        Public Property GetInVal() As Integer 
            Get 
                Return _GetInVal  
            End Get 
            Set(ByVal value As Integer)  
                If _GetInVal > 0 Then 
                    _GetInVal = value  
                Else : Throw New Exception("InValue can not be less then 1")  
                End If 
            End Set 
        End Property         
     
     

    you need to change this to:

       
        Public Property GetInVal() As Integer  
            Get  
                Return _GetInVal  
            End Get  
            Set(ByVal value As Integer)  
                If value > 0 Then  
                    _GetInVal = value 
                Else : Throw New Exception("InValue can not be less then 1")  
                End If  
            End Set  
        End Property         
     
     

    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Tuesday, March 17, 2009 12:30 PM

All replies

  •   Private _GetInVal As Integer

    You created the variable but it is uninitialized so it does not equal anything . Your set method won't let it be set to anything since _GetInVal is nothing . I think Nothing is also -1 but either way your set method can not work . Shouldn't you be checking value not   _GetInVal ? value is what the method is trying to set GetInValue to shouldn't it be  

    If value > 0 Then
    _GetInVal = value

    . You can give _GetInVal a value when you create the variable as well .

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    • Proposed as answer by Heslacher Tuesday, March 17, 2009 7:02 AM
    • Marked as answer by Martin Xie - MSFT Monday, March 23, 2009 3:05 AM
    Tuesday, March 17, 2009 1:05 AM
  • Hi,

    Change

    clsMath.GetInVal = CInt(txtEnter.Text)

    to


     
    integer.tryparse(txtEnter.Text,clsMath.GetInVal) 
     
     

    so as to avoid errors if the user does NOT type a number ( in error ).


    Regards,

    John

    Tuesday, March 17, 2009 5:18 AM
  • Hi Zixing Wang,

    it is like bdbodger says. You are checking wether _GetInVal is >0, but you never assigned any value to it. I think you wanted to check if value>0.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Tuesday, March 17, 2009 7:02 AM
  • The value that is >1 was assigned through an data entry text box txtEnter:
    clsMath.GetInVal = CInt(txtEnter.Text),

    So the property GetInVal of class clsMath receives the entered value from txtEnter.text.
    That is why when mouse over the value in SET section shows the entered value.

    Also the private _GetInVal was initialated (see 2nd line of program). The problem is why
    in _GetInVal = value assignement, the variable _GetInVal is unable to get the data from value (the mouse over value shows an integer that is > 1)

    Thanks

    Zixing


    zixing wang
    Tuesday, March 17, 2009 12:26 PM
  • Hi Zixing Wang,

    like i mentioned before, your problem comes from your Property

          
        Public Property GetInVal() As Integer 
            Get 
                Return _GetInVal  
            End Get 
            Set(ByVal value As Integer)  
                If _GetInVal > 0 Then 
                    _GetInVal = value  
                Else : Throw New Exception("InValue can not be less then 1")  
                End If 
            End Set 
        End Property         
     
     

    you need to change this to:

       
        Public Property GetInVal() As Integer  
            Get  
                Return _GetInVal  
            End Get  
            Set(ByVal value As Integer)  
                If value > 0 Then  
                    _GetInVal = value 
                Else : Throw New Exception("InValue can not be less then 1")  
                End If  
            End Set  
        End Property         
     
     

    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Tuesday, March 17, 2009 12:30 PM
  • Hi Heslacher,

    Well spotted!! Forum user bdbodger also spotted it, see the 1st reply in this thread.

    That is why I have proposed your post as answer.

    :-)

    I would still also do the change I suggested earlier too.
    Reason: If the user accidentally types a letter or something that is not a number into the txtEnter TextBox then

    CInt(txtEnter.Text)

    will throw an EXCEPTION error.

    Whereas this line of code will try to convert ( i.e. TryParse ) the text in the txtEnter TextBox into the INTEGER property that is clsMath.GetVal >>



    integer.tryparse(txtEnter.Text,clsMath.GetInVal)



    The result will be a zero to throw the custom exception in the SET section of the property if a zero or if anything else other than a number is typed.


    Regards,

    John

    • Proposed as answer by Heslacher Tuesday, March 17, 2009 1:21 PM
    • Edited by John Anthony Oliver Tuesday, March 17, 2009 1:57 PM Changed Heschaler to Heslacherr
    • Marked as answer by Martin Xie - MSFT Monday, March 23, 2009 3:04 AM
    Tuesday, March 17, 2009 1:10 PM
  • Hi John,

    you are right.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Tuesday, March 17, 2009 1:20 PM
  • Heslacher said:

    Hi John,

    you are right.


    Mark the thread as answered if the answer helps you. This helps others who have the same problem !


    Hi again Heslacher,

    Thanks for your support!!

    :-D    ;-)    :-)

    To Zixing Wang,

    Do you want to see both suggested corrections in one post or are you okay to change them yourself?


    Regards,

    John

    • Edited by John Anthony Oliver Tuesday, March 17, 2009 2:01 PM ____________________________________________________________________
    Tuesday, March 17, 2009 1:55 PM
  • Thanks a lot. I think you are right. I just ignor the syntex  GetInVal >0 is wrong.

    It shall value >0. The reason I ignored this syntex is that I copied it from an example of a VB.NET book and believe that the author should run his program sample before publishing it as a sample codes of his book.

    Thanks again,

    Zixing

     


    zixing wang
    Thursday, March 19, 2009 7:18 PM