views:

336

answers:

4

Let's say you have a class with a Uri property. Is there any way to get that property to accept both a string value and a Uri? How would you build it?

I'd like to be able to do something like one of the following, but neither are supported (using VB, since it lets you specify type in the Set declaration for the 2nd one):

Class MyClass

    Private _link As Uri

   ''//Option 1: overloaded property
    Public Property Link1 As Uri
        Get
            return _link
        End Get
        Set(ByVal value As Uri)
           _link = value
        End Set
    End Property

    Public Property link1 As String
        Get
            return _link.ToString()
        End Get
        Set(Byval value As String)
           _link = new Uri(value)
        End Set
   End Property

   ''// Option 2: Overloaded setter
   Public Property link2 As Uri
      Get
          return _link
      End Get
      Set(Byval value As Uri)
          _link = value
      End Set
      Set(Byval value As String)
          _link = new Uri(value)
      End Set
End Class

Given that those probably won't be supported any time soon, how else would you handle this? I'm looking for something a little nicer than just providing an additional .SetLink(string value) method, and I'm still on .Net2.0 (though if later versions have a nice feature for this, I'd like to hear about it).

I can think of other scenarios where you might want to provide this kind of overload: a class with an SqlConnection member that lets you set either a new connection or a new connection string, for example.

+3  A: 

I think you just need to provide an accompanying

Public Sub SetLink(ByVal value as String)
    _link = new Uri(value)
End Sub

Nothing nicer is available, AFAIK.

bdukes
I think you posted at the same time I was editing the question.
Joel Coehoorn
Also, I'd implement the .SetLink() to use the property rather than set field directly, in case the property has an side effects.
Joel Coehoorn
+1  A: 

Let's say you have a class with a Uri property. Is there any way to get that property to accept both a string value and a Uri?

No because this would mean having two getters that vary only in their return type and this isn't allowed in .NET.

I would use the Uri method exclusively and perhaps create a convenienec method to set the URI property, given a string. However, since the conversion from String to URI is straightforward, even this might be unnecessary.

Konrad Rudolph
I was thinking only one getter that always returns Uri. But, yeah, I'm realizing that MyClass.MyUriProperty = new Uri("http://example.com") is pretty simple
Joel Coehoorn
+1  A: 

You can't have one property like that, but you could create two properties which both manipulated the same underlying field - just like Height/Width/Size in Windows Forms.

Jon Skeet
+2  A: 

Alternatively, you can of course forego overloading and just name the properties appropriately:

Public WriteOnly Property UriString() As String
    Set(ByVal value As String)
        m_Uri = new Uri(value)
    End Set
End Property

Of course you don't have to make this WriteOnly but it seems appropriate.

Konrad Rudolph
I probably wouldn't go WriteOnly; if we're going to go this route, go all the way and provide a read version like:Get Return m_Uri.ToString()End Get
John Rudy
Accepted this answer because I ultimately also need this class to be serializable, which means having a _property_ that the serializer can see and use.
Joel Coehoorn