Question Public Properties

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
What is the purpose of Public property? why would i do this instead of just doing Public abc as string?

VB.NET:
    Dim abc As String
    Public Property abcValue() As String
        Get
            Return abc
        End Get
        Set(ByVal value As String)
            abc = value
        End Set
    End Property
Because you may want to do data validation on it before assigning the value to the Private variable. Because you might want to raise an event when the value in the Private variable changes. Because you might later want to make it a ReadOnly property. Because....

Would you like me to continue?
 
What is the purpose of Public property? why would i do this instead of just doing Public abc as string?

VB.NET:
    Dim abc As String
    Public Property abcValue() As String
        Get
            Return abc
        End Get
        Set(ByVal value As String)
            abc = value
        End Set
    End Property
Because you may want to do data validation on it before assigning the value to the Private variable. Because you might want to raise an event when the value in the Private variable changes. Because you might later want to make it a ReadOnly property. Because....

Would you like me to continue?
 
I knew that was coming, but you do it better than I. I was using subtlety, thanks JB.

PS I know that it does not look like its spelled right, but it is. :)
 
I like the idea of Auto-Implemented Properties that will arrive with VS 2010, this will simplify much of this code. Using the full property declaration will of course still be the only option when you need to do several things in setter/getter.
 
I like the idea of Auto-Implemented Properties that will arrive with VS 2010, this will simplify much of this code. Using the full property declaration will of course still be the only option when you need to do several things in setter/getter.

This is such a wonderful feature that I always miss when coming back to 2005. (It's in the budget for next year :D)

It's probably common knowledge but a nice shortcut is to type 'Property' and hit tab to insert the expanded property snippet.
 
Last edited:
I know about this, typing 'prop'-Tab-Tab is sufficient, but you still have to 'fill in the blanks' field name, property name and type, and the code itself expands to nine lines of code when only one was "needed" :eek:
 
If i have an array

VB.NET:
dim ds() as dataset
how do i add a public property for it? I tried

VB.NET:
    Public Property dsValue(ByVal index As Integer) As DataSet
        Get
            Return ds(index)
        End Get
        Set(ByVal value As DataSet)
            ds(index) = value
        End Set
    End Property

but this seem to be missing some of the action like redim the array or getting the length of the array
 
Last edited:
If i have an array

VB.NET:
dim ds() as dataset
how do i add a public property for it? I tried

VB.NET:
    Public Property dsValue(ByVal index As Integer) As DataSet
        Get
            Return ds(index)
        End Get
        Set(ByVal value As DataSet)
            ds(index) = value
        End Set
    End Property

but this seem to be missing some of the action like redim the array or getting the length of the array
You don't add properties to arrays. An array is an object. You don't add properties to objects. You add properties to types when you define them. An object you create based on that type will then have that property. An array is an instance of the Array class. You didn't define the Array class so you don't get to decide what properties it has.

Apart from that, your property doesn't really make sense because arrays are already indexable, so you can already assign an object to an element by index. Of course, the size of the array has to be such that that index is valid. If you want an array-like object that can grow dynamically then you can use a collection, e.g. a generic List. Even then though, you can't set an item at an index that doesn't exist. If you wanted to be able to do that then you'd have to define your own type, so that you CAN define your own properties and your own behaviour.
 
Ah, OK... now I think I see what you're trying to achieve. I thought you meant that you wanted to add a property TO the array, not to expose the array outside some other object.

One of the reasons that would use a property rather than a field in that case is to prevent a caller completely replacing your array. If you use a field then a caller can simply assign a whole new array to it, thus throwing away your existing array. By using a property you can declare it ReadOnly, thereby providing access to the existing array without allowing it to be replaced.

That said, there might be cases where you do want the array to be able to be replaced. Consider the TextBox.Lines property for instance. In that case though, extra processing is required within the property getter and setter, meaning that a field couldn't be used anyway.

In most cases, if you want to expose a list of objects outside another object, you would use a collection that is exposed via a ReadOnly property. There is a vast number of examples of this throughout the Framework, e.g. Form.Controls, DataSet.Tables, DataTable.Rows, ComboBox.Items, DataGridView.Columns, etc, etc.
 
Back
Top