Control Property.

Control Property.

am 30.12.2007 03:10:19 von Shapper

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

RE: Control Property.

am 30.12.2007 03:36:00 von pbromberg

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


"shapper" wrote:

> 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
>

RE: Control Property.

am 30.12.2007 03:55:00 von mily242

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.
--
Milosz


"shapper" wrote:

> 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
>

Re: Control Property.

am 30.12.2007 09:23:35 von Riki

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

shapper wrote:
> 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

--
Riki