locked
Class Property Syntax help needed please RRS feed

  • Question

  • User433556381 posted

    I am trying to use my properties to retrieve data as directly from controls but I am not sure if I have the right syntax here. PLease help me figure out what is wrong with the code below. Thanks in Advance

     

    Private _birthdate As Date

    Public Property BirthDate() As Date

    Get

    Return _birthdate

    End Get

    Set(ByVal value As Date)

    If TxtBDate.Text IsNot Nothing Then

    _birthdate = CType(Me.TxtBDate.Text.Trim, Date)

    Else

    _birthdate = value

    End If

    End Set

    End Property

    Tuesday, May 20, 2008 1:56 PM

Answers

  • User-125547262 posted

    Do you really need a set method? since you are not setting the textbox's value

    Public ReadOnly Property BirthDate() as Nullable(of Date)

    Get

     if Not string.isNullorEmpty(TxtBdate.Text) andalso IsDate(TxtBDate.Text) then

    return CType(me.TxtBDate.Text.Trim,Date)

    else

    return nothing 

    End Get

    End Property
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 20, 2008 2:53 PM

All replies

  • User-417784260 posted

    When you assign a value to a property you will be getting value in the set procedure.  There is no reason convert the value in a textbox to a date there. 

     

    Set(ByVal value As Date)

    If TxtBDate.Text IsNot Nothing Then

    _birthdate = CType(Me.TxtBDate.Text.Trim, Date)

    Else

    _birthdate = value

    End If

    End Set

     

     

    Your property should be like this

     

     Set(ByVal value As Date)

    _birthdate = value

    End If

    Tuesday, May 20, 2008 2:51 PM
  • User-125547262 posted

    Do you really need a set method? since you are not setting the textbox's value

    Public ReadOnly Property BirthDate() as Nullable(of Date)

    Get

     if Not string.isNullorEmpty(TxtBdate.Text) andalso IsDate(TxtBDate.Text) then

    return CType(me.TxtBDate.Text.Trim,Date)

    else

    return nothing 

    End Get

    End Property
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 20, 2008 2:53 PM