Reputation: 10236
ok, maybe it's not a method exactly. here's what I'd like to do:
Private _Columns As ArrayList
Public Property Columns() As ArrayList
Get
Return _Columns
End Get
Set(Cols As ArrayList)
_Columns = Cols
End Set
Set(Cols As MyOwnList)
_Columns = New ArrayList
For Each o As MyObj in Cols
_Columns.Add(o.Column)
Next
End Set
End Property
which would allow me to conveniently set the property by assigning to it various kinds of collection types... except, I can't do it because apparently the Set method has to take an argument of the type of the property itself... I get the complaint:
'Set' parameter must have the same type as the containing property
is there a way to do this?
Upvotes: 2
Views: 1725
Reputation: 14133
It's not really overloading but the following could offer a (temporary) workaround:
Private Cols As ArrayList
Public Property Columns() As ArrayList
Get
Return _Columns
End Get
Set(Cols As ArrayList)
_Columns = Cols
End Set
End Property
Public WriteOnly Property SpecializedColumns() As MyOwnList
Set(value As MyOwnList)
Dim list As New ArrayList
For Each o As MyObj In value
list.Add(o.Column)
Next
Columns = list
End Set
End Property
I think this is as close as you can get to real overloaded properties...
EDIT
I believe the proper way to do what you're trying to achieve is to define a ToArrayList()
method on your MyOwnList
class.
Public Function ToArrayList()
Dim list As New ArrayList
For Each o As MyObj In Me.Items
list.Add(o.Column)
Next
Return list
End Function
and set your property like this:
Columns = theMyOwnListObject.ToArrayList
If you don't have access to the source code of the class, you can still achieve this through extension methods.
Upvotes: 3
Reputation: 3363
No, you can't change the type of the parameter expected by the Setter in your auto-property.
However, you could supply your own implicit casting function for MyOwnList from your example and then it would automatically be converted into an ArrayList when you pass it in to the setter.
Class MyOwnList
' your class code here....
'
Public Shared Widening Operator CType(ByVal p1 As MyOwnList) As ArrayList
Dim columns As New ArrayList
For Each o As MyObj in p1
columns.Add(o.Column)
Next
return columns
End Operator
End Class
Then elsewhere in your program this code would work:
Dim myList as new MyOwnList
SomeClass.Columns = myList
Upvotes: 3