Hello,
I am creating a Composite Control, named Form, which contains a
TextBox.
Form control has a property named Value which define the TextBox text
property.
When I define it as follows it does not work:
Public Property Value() As String
Get
Return ViewState("Value")
End Get
Set(ByVal value As String)
ViewState("Value") = value
End Set
End Property ' Value
Private Sub tbInput_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles tbInput.Init
tbInput.Text = Me.Value
End Sub ' tbInput_Init
Then I changed this simply to:
Public Property Value() As String
Get
Return tbInput.Text
End Get
Set(ByVal value As String)
tbInput.Text = value
End Set
End Property ' Value
Aren't both approaches the same?
Thanks,
Miguel
Peter Bromberg [C# MVP] - 30 Dec 2007 02:36 GMT
I'd be very wary of creating properties that have reserved keywords as their
names, e.g. "Value" - especially in VB.NET which is case-insensitive.
"Text" would be the more appropriate name for the property.
No, they are not the same, since the first uses ViewState to store and
retrieve the value across page postbacks.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
> Hello,
>
[quoted text clipped - 35 lines]
> Thanks,
> Miguel
Milosz Skalecki [MCAD] - 30 Dec 2007 02:55 GMT
Howdy,
No they aren't. The first sets the text in the init therefore changing the
text after this event would not be reflected. Secondly, the text would be
stored in viewstate twice (in your control and child textbox). In addition,
there's a small bug in the second approach as you have to make sure child
controls have been created before accessing any of their properties:
Public Property Value() As String
Get
EnsureChildControls()
Return tbInput.Text
End Get
Set(ByVal value As String)
EnsureChildControls()
tbInput.Text = value
End Set
End Property ' Value
I'd use the second definition.

Signature
Milosz
> Hello,
>
[quoted text clipped - 35 lines]
> Thanks,
> Miguel
Riki - 30 Dec 2007 08:23 GMT
Another difference, not mentioned by Peter and Milosz, is that
Return ViewState("Value")
sometimes can return Nothing, whereas the Text value of a TextBox can not.
Jos
> Hello,
>
[quoted text clipped - 35 lines]
> Thanks,
> Miguel

Signature
Riki