What is uses of "Default" & "Tag" in vb.net?

tommy_cs

Member
Joined
Jun 7, 2006
Messages
18
Programming Experience
3-5
what is the function of "Default"?
Default Public ReadOnly Property Item(ByVal Index As Integer) As ...


And what is the uses of Tag property?
dim index as integer = CType(eventSender, System.Windows.Forms.CheckBox).Tag

Thanks!:confused:
 
Tag is basicly a place that you can store any information about the object you like.

the default property is what will be used if you just put a open bracket after the object for example...

Listview1(0).text = "test"
is the same as...
Listview1.Items(0).text = "test"

because Items is the default property
 
Pleh is correct and incorrect. Items is not the default property of the ListView class. In fact Items returns a ListViewItemCollection object, for which the default property is Item. That means that you can do this:
VB.NET:
myListView.Items(0)
instead of this:
VB.NET:
myListView.Items.Item(0)
You will find that almost every collection type has an Item property that is the default property. If not it will have another property that is equivalent. The purpose is to allow you to treat a collection just like an array. Note that to be the default a property MUST have at least one argument.
 
In addition if you wan this to be the default member of this collection, you will need the following attribute at the top of the class..

VB.NET:
<System.Reflection.DefaultMember("Item")> _
 
That's actually not true. When you declare a property with the Default keyword it automatically becomes the default member and the compiler emits the attribute automatically. Note that it is illegal to declare a property with the Default keyword and specify another member as the default member.
 
Did not know that:) I thought that the default keyword was used instead of the DefaultProperty Attribute not the DefaultMember Attribute since i've never used the default keyword i would never have known. You learn something new everyday:)
 
Probably does it in the background, like it does with the default constructor - if you dont provide a constructor, the compiler write an empty, no-op constructor for you when compiling.

Of course, you never see it in your code, and the only time i've ever seen it cause confusion is in inheritance (and even then it was so long ago that I cannot remember exactly what it was.. something to do with the default constructor not being provided in a base class if another constructor is present, and hence unavailable for a child class to call.. but if the child class one was being put implicitly by the constructor, then people never worked out why their child class threw a compile error that the base constructor was absent)
 
The DefaultProperty attribute is something else entirely. The property specified as the DefaultProperty is the one that is selected by default in the Properties window of the Windows Forms designer. The DeafaultEvent attribute does the same thing for an event when the Properties window is showing events instead of properties. That's why both those classes are members of the System.ComponentModel namespace: because only components can be seen in the designer so only components can have a DefaultProperty and DefaultEvent. The DefaultMemberAttribute class is a member of the System.Reflection namespace becvause it relates to how a Type behaves when used with reflection.
 
Back
Top