开发者

Databind to object parameter that has one or more optional parameters

开发者 https://www.devze.com 2023-02-18 20:17 出处:网络
I have an object, called \'PERSON\' This person object has a title, firstName & Surname property as well as many other which are at the moment irrelevant. It also has a read only property called

I have an object, called 'PERSON'

This person object has a title, firstName & Surname property as well as many other which are at the moment irrelevant. It also has a read only property called Fullname which concatenates the two or three parameters mentioned above depending on an optional parameter 'withTitles' passed over when you call PERSON.FULLNAME

PERSON.FULLNAME(true) <- Will add titles if there are any

PERSON.FULLNAME(false) <- Will give the name without the title

Public ReadOnly Property FullName(Optional ByVal withTitle As Boolean = False) As String
            Get
                    Dim _ttle As String = Me.Title
                    Select Case withTitle
                        Case True
     开发者_JAVA百科                       If _ttle.Length > 0 Then _ttle += " " Else _ttle = String.Empty
                        Case False
                            _ttle = String.Empty
                    End Select
                    If Me.FirstName <> "" And Me.LastName <> "" Then
                        Return _ttle & Me.FirstName & " " & Me.LastName
                    ElseIf Me.FirstName = "" And Me.LastName <> "" Then
                        Return _ttle & Me.LastName
                    ElseIf Me.FirstName <> "" And Me.LastName = "" Then
                        Return _ttle & Me.FirstName
                    ElseIf Me.FirstName = "" And Me.LastName = "" Then
                        Return Me.ContactName
                    End If
              End Get
        End Property

My problem surfaces when I try to bind my PERSONCOLLECTION (a collection of the PERSON object) to a RadioButtonList or any other binding control at that.

        RadioButtonList1.DataSource = _personCollection

        RadioButtonList1.DataTextField = "FullName"
        RadioButtonList1.DataValueField = "ContactID"
        RadioButtonList1.DataBind()

I get an error: PERSON does not contain a property of 'FullName'. If I change this to any other property that does not take a parameter it works as expected.

Now I'm guessing that the binding procedure can't handle optional or mandatory parameters for object properties, is this right? Is there a better way to do it?

I thought about looping through the collection to add them manually but that kinda defeats the object of DataBinding!

Any help would be apreciated. Kev.


I would recommend creating two properties, FullName and FullNameWithTitle. Although properties can take parameters they are intended to be indexers into the object and not actionable values. For instance a class that represents a collection of Color objects might have an Item property with an optional index being the specific color to return.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号