I give up on VB.NET
Google Search: DefaultValue Attribute VB.NET
It really IS the bastard stepchild of .NET. It’s just so much easier to code in C# than it is in VB.NET.
I’m writing a custom web control. I have a property called “TabItems” which is an ArrayList holding, TabItem classes. I want to set a Default Value. How do I do that? The only example I can find in the MSDN docs uses a boolean property and sets it like so.
<defaultvalue (False)> _
Public Property MyProperty() As Boolean
yeah, duh no kidding. The only other examples I can find use string properties and set the DefaultValue to “”. DUH. What about an actual type?
I’m going back to C# now, let somebody else play in the shallow muddy end of the .NET pool with VB.
edited because I forgot that WordPress doesn’t URLencode the posts

March 27th, 2004 - 23:53
Isn’t this what you needed? http://www.abstractvb.com/code.asp?A=1036
March 27th, 2004 - 23:58
Oops. I posted the wrong one. The one you found was this one:
http://www.abstractvb.com/code.asp?A=960 which had a typo; that wasn’t VB.NET’s fault;
it was the fault of abstractvb.com.
Here’s how you do it in C#: http://www.abstractvb.com/code.asp?A=999
To do in VB, just use VB’s attribute syntax instead. The capabilities of
VB.NET and C# are practically identical, the syntaxes are just different.
It really is just a matter of personal preference.
March 28th, 2004 - 01:38
Mike,
I think for the most part that’s true. The syntax is the same, but the usage is a little different.
Here’s the code I was working on in both VB and C#
[Bindable(true),Category("Items"),DefaultValue(null)]
public System.Collections.ArrayList TabItems
{
get { return this._tabItems; }
set { this._tabItems = value; }
}
<Bindable(False), Category(“Data”), DefaultValue(Nothing)> Property TabItems() As ArrayList
Get
Return _tabItems
End Get
Set(ByVal Value As ArrayList)
_tabItems = Value
End Set
End Property
The difference is, the C# version compiles and works. The VB one doesn’t. I want use the DefaultValue attribute to set a (duh) default value of null (nothing) on the TabItems property in the designer. What do I pass into the arguments for the DefaultValue attribute in VB? I tried “typeof(ArrayList)” I tried various combinations of GetType(typeof()) and so forth. The MSDN docs couldn’t tell me anything, every example either use boolean properties or string properties. I wanted to see an example of a property with a reference type, besides string, that used the DefaultValue attribute. I still haven’t figured it out, it was easy in C# so I just re-wrote the control in C#. It took about 10 minutes for me to re-write it.
I started out dabbling in VB4 32bit, moved on to VB 5, and started using VB 6.0 while it was in beta. They’ve really screwed up VB, it can’t really be considered a RAD development tool anymore. Hopefully the Whidbey release will go a long ways towards fixing that situation.
March 28th, 2004 - 01:58
Hmm. Well the default value for a object *is* “Nothing,” and there is no constant for any other valid object state, so maybe they don’t thing they need to implement on non-value types? If you *don’t* include the DefaultValue() as you were trying, does it not work for what you need? Or are you just trying to get it to work for a datatype that is not string or number for learning purposes?
October 19th, 2004 - 07:34
Try this:
Default Public Property PropName(ByVal Value As Integer) as Integer
Get
Return _MemberVariable
End Get
Set
_MemberVariable = Value
End Set
End Property
April 28th, 2005 - 08:33
In my case, I did it with an image
DefaultValue(GetType(Image), Nothing)
Compiles and works fine.
Pete
July 11th, 2006 - 21:06
The way to do this is as follows:
For other reference types, simply replace the GetType() value. For example, to set a default value for a string property: