属性集重载数组与非数组

发布于 2024-08-20 18:20:02 字数 842 浏览 3 评论 0原文

我正在尝试创建一个属性,允许根据各种输入设置姓氏。调用此函数的代码正在解析名称字段。在某些情况下,它有一个姓氏,而在其他情况下,它有一个需要连接的姓氏数组。我想将串联代码保存在一个位置以便于维护。

这是我的代码,当然不起作用:

        Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Public WriteOnly Property LastName() As String()
        Set(ByVal value As String())
            For Each word As String In value
                _LastName &= word & " "
            Next

            _LastName = _LastName.Trim
        End Set
    End Property

我已经尝试了多种变体,甚至将多个 Set 放在同一个属性中,但似乎没有任何效果。必须有某种方法来获取数组和单个实例值。编译器清楚地知道传入的对象是否是数组。

我尝试仅使用 String() 类型的属性,但无法返回字符串。我能想到的唯一解决方案是使用不同名称的属性,这使得使用类变得更加困难,或者一大堆 Subs 和 Functions 相互重载。

建议?

克里斯

I'm trying to create a property that will allow the setting of a last name based on a variety of inputs. The code that calls this is parsing a name field. In some instances it will have a single last name, in others, a array of last names that need to be concatenated. I want to keep my concatenation code in a single place to make maintenance easier.

Here is my code, which of course doesn't work:

        Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Public WriteOnly Property LastName() As String()
        Set(ByVal value As String())
            For Each word As String In value
                _LastName &= word & " "
            Next

            _LastName = _LastName.Trim
        End Set
    End Property

I've tried numerous variations on this, even putting multiple Set in the same Property, but nothing seems to work. There must be some way to take in an array and a single instance value. The compiler clearly knows if the incoming object is an array or not.

I tried working with just a property of type String(), but then I can't return a String. The only solution that I can think of is differently named properties, which makes using the class more difficult, or a whole bunch of Subs and Functions overloading each other.

Suggestions?

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很糊涂小朋友 2024-08-27 18:20:02

您不能重载属性,因此属性可以是 stringstring(),但不能同时使用两者。

考虑为 string() 属性使用不同的名称:

Public WriteOnly Property LastNameArray() As String()
    Set(ByVal value As String())
        For Each word As String In value
            _LastName &= word & " "
        Next

        _LastName = _LastName.Trim
    End Set
End Property

You cannot overload properties, so the property can by either a string or a string(), not both.

Consider having a different name for the string() property:

Public WriteOnly Property LastNameArray() As String()
    Set(ByVal value As String())
        For Each word As String In value
            _LastName &= word & " "
        Next

        _LastName = _LastName.Trim
    End Set
End Property
梦境 2024-08-27 18:20:02

如果您的输出与输入的类型不同,则不能为其使用属性。使用函数并没有什么问题,好的做法是你有函数名称 (LastName),并在其前面添加一个动词 (Get),所以你最终会得到一个类似 GetLastName 的函数。

Public  Function GetLastName(ByVal names As String()) As String

(如果有什么明显的错误,请原谅我生锈的 VB)

If your output is going to be of a different type than your input, then you cannot use a property for it. There is nothing wrong with using a function for it, good practice would say that you have the function name (LastName), and prepend a verb on to it (Get), so you end up with a function anme like GetLastName.

Public  Function GetLastName(ByVal names As String()) As String

(excuse my very rusty VB if there is something blatantly wrong)

最后的乘客 2024-08-27 18:20:02

像这样的事情怎么样:

        Public Property LastName() As Object
        Get
            Return _LastName
        End Get
        Set(ByVal value As Object)
            If TypeOf (value) Is Array Then
                For Each word As String In value
                    _LastName &= word & " "
                Next

                _LastName = _LastName.Trim

            End If
            If TypeOf (value) Is String Then
                _LastName = value
            End If
        End Set
    End Property

有人认为这有问题吗?

What about something like this:

        Public Property LastName() As Object
        Get
            Return _LastName
        End Get
        Set(ByVal value As Object)
            If TypeOf (value) Is Array Then
                For Each word As String In value
                    _LastName &= word & " "
                Next

                _LastName = _LastName.Trim

            End If
            If TypeOf (value) Is String Then
                _LastName = value
            End If
        End Set
    End Property

Does anyone see a problem with this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文