Results 1 to 3 of 3

Thread: Get Set Property help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Get Set Property help

    How can i get and set the value in asp.net page.

    Public Property KeyValue As String

    Get

    Dim o as Object = ViewState("KeyValue")

    If o Is Nothing Then

    Return ""

    Else

    Return CType(o, String)

    End If

    End Get

    Set (ByVal Value as String)

    ViewState("KeyValue") = Value

    End Set

    End Property

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633

    Re: Get Set Property help

    What's wrong with the above???
    VB Code:
    1. Private Property MyItem() As String
    2.         Get
    3.  
    4.         End Get
    5.         Set(ByVal Value As String)
    6.  
    7.         End Set
    8.     End Property
    However, I would write a few overloaded functions to be used with this property:
    VB Code:
    1. Private Property MyItem() As String
    2.         Get
    3.             Return Me.GetViewState("MyItem", String.Empty)
    4.         End Get
    5.         Set(ByVal Value As String)
    6.             Me.SetViewState("MyItem", Value)
    7.         End Set
    8.     End Property
    9.  
    10.     Private Overloads Function SetViewState(ByVal Key As String, ByVal Value As String)
    11.         Me.ViewState.Item(Key) = Value
    12.     End Function
    13.  
    14.     Private Overloads Function SetViewState(ByVal Key As String, ByVal Value As Integer)
    15.         Me.ViewState.Item(Key) = Value
    16.     End Function
    17.  
    18.     Private Overloads Function SetViewState(ByVal Key As String, ByVal Value As Boolean)
    19.         Me.ViewState.Item(Key) = Value
    20.     End Function
    21.  
    22.     Private Overloads Function GetViewState(ByVal Key As String, ByVal DefaultValue As String)
    23.         Dim ret As String = DefaultValue
    24.  
    25.         If Not (Me.ViewState.Item(Key) Is Nothing) Then
    26.             ret = CType(Me.ViewState.Item(Key), String)
    27.         End If
    28.  
    29.         Return ret
    30.     End Function
    31.  
    32.     Private Overloads Function GetViewState(ByVal Key As String, ByVal DefaultValue As Integer)
    33.         Dim ret As Integer = DefaultValue
    34.  
    35.         If Not (Me.ViewState.Item(Key) Is Nothing) Then
    36.             ret = CType(Me.ViewState.Item(Key), Integer)
    37.         End If
    38.  
    39.         Return ret
    40.     End Function
    41.  
    42.     Private Overloads Function GetViewState(ByVal Key As String, ByVal DefaultValue As Boolean)
    43.         Dim ret As Boolean = DefaultValue
    44.  
    45.         If Not (Me.ViewState.Item(Key) Is Nothing) Then
    46.             ret = CType(Me.ViewState.Item(Key), Boolean)
    47.         End If
    48.  
    49.         Return ret
    50.     End Function
    Hope that helps.

    Woka

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width