Scott Scott - 1 year ago 95 Question

How to add a "sub property" to a class property

If, in code, I wanted to do something like the following, what would my class definition need to look like? (Keep in mind the fruit/language thing is just an example)

dim myfruit as new fruit() = "apple" = "manzana"

Here is the class I have, just not sure how to add the "sub property".

Public Class Fruit
Private _name As String
Public Property name() As String
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class

Answer Source

In general, for you to have a "sub property", you'd need to make your Property a class itself. This would mean the subproperty is actually a property on the class exposed by the top level property.

Effectively, you'd change the name property from a string to a "Translations" class or similar, ie:

Public Class Fruit
    Public Property Name As New Translations
End Class

Public Class Translations
    Public Property Primary As String
    public Property Spanish As String
End Class

However, this will likely break the code you're displaying, as the second line would need to have a different syntax, ie:

myfruit.Name.Primary = "green"
myfruit.Name.Spanish = "verde"

However, if the goal here is to just handle translation of your user interface, there are other options. For details, see Introduction to International Applications Based on the .NET Framework on MSDN.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download