Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Control Property.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 30 Dec 2007 02:10 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.