Hi,
I have a custom Attribute class where the constructor takes one fixed parameter and two optional parameters. It is my understanding that one can supply one or more optional parameters by using 'named parameters' such as this:
This way I skip the 'y' parameter and simply let it use the default value of 0. This works fine for any class, except apparently for attributes...Code:Private Sub CallMethod() Me.OptionalMethod(-1, z:=1) End Sub Private Sub OptionalMethod(ByVal x As Integer, Optional ByVal y As Integer = 0, Optional ByVal z As Integer = 10) '... End Sub
This is my attribute class:
vb.net Code:
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=False)> _ Public Class GridOptionsAttribute Inherits System.Attribute Public Sub New(ByVal visible As Boolean, Optional ByVal text As String = Nothing, Optional ByVal index As Integer = -1) _DisplayName = text _Visible = visible _DisplayIndex = index End Sub Private _DisplayName As String Public ReadOnly Property DisplayName() As String Get Return _DisplayName End Get End Property Private _Visible As Boolean Public ReadOnly Property Visible() As Boolean Get Return _Visible End Get End Property Private _DisplayIndex As Integer Public ReadOnly Property DisplayIndex() As Integer Get Return _DisplayIndex End Get End Property End Class
When I try to use it, I cannot seem to use named parameters:
Huh?
Well, I thought, perhaps Attributes don't support named parameters. I know they can behave a little weird (they don't support all types in their arguments for example I think), so I thought little of it.
But then... I noticed something else. If you take a look at my code of the attribute class again. Notice that I am using an attribute on that class as well... And what do you know: named parameters. Working just fine... The AttributeUsage attribute has the exact same configuration: one fixed parameter and two optional parameters. I seem to be able to call them using named parameters just fine, where I cannot call my own attribute constructor with named parameters...
What's going on... Am I losing my mind? Is VS bugged? Or is it infected with a demon?![]()





Reply With Quote