user736893
user736893

Reputation:

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()
myfruit.name = "apple"
myfruit.name.spanish = "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
        Get
            Return _name 
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class

Upvotes: 2

Views: 2755

Answers (2)

SteveCinq
SteveCinq

Reputation: 1973

I initially thought Reed´s answer was what I was after. In my application, I wanted to use the "sub-property" to set a property on a Form Label. (I was trying to emit only the Label properties I wanted available to a Custom Control.)

I tried this:

Public Class Fruit
    Private _name As New Translations
    Public Property Name As Translations
        Get
            Return _name
        End Get
        Set(value As Translations)
            _name = value
            _PrimaryCaps = _name.Primary.ToUpper
        End Set
    End Property
    'Private variable is automatically added for unexpanded property
    Public Property PrimaryCaps As String
End Class

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

Then

Dim myFruit As New Fruit
myFruit.Name.Primary = "Apple"
myFruit.Name.Spanish = "Manzana"
Dim primaryCaps As String = myFruit.PrimaryCaps

Weirdly - to me at least - this doesn't work; myFruit.PrimaryCaps returns nothing rather than the hoped-for "APPLE". It appears that the Set for Name is never executed. (Placing the _PrimaryCaps assignment above the Get Return does work, however.)

(I realize that a PrimaryCaps property could be added to the Translations class but, again, this doesn't help if you're wanting to set a foreign variable from within an instance of Fruit.)

I don't know if this is "by-design", whether I've simply misunderstood the intended functionality or what. One thing I did alight on after further research was that this structure isn't very common at all in .NET; for example setting a control's size is done as follows:

oControl.Size = New Drawing.Size(20, 15)

rather than simply setting, say, the Width property directly:

oControl.Size.Width = 20

(The latter won't compile: "Expression is a value and therefore cannot be the target of an assignment.")

If anyone has any more insight than I on this, I'd love to hear it. I know this could simply be done by using an instance of Fruit, for example, but that's not the point.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564911

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.

Upvotes: 4

Related Questions