Results 1 to 17 of 17

Thread: How to create a Property.subProperty?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Question How to create a Property.subProperty?

    Hi Guys,

    My final goal is to have a property contain 2 values, the first value will be used the normal way you would in a property,
    Eg.
    Code:
    myClass.myProperty = "myName"
    
    dim str as String = myClass.myProperty
    and the second value, will be metaData,
    Eg.
    Code:
    myClass.myProperty.metaData = "mySurname"
    
    dim str as string = myClass.myProperty.metaData
    -----------------------------------------------------------

    Here is where I'm up to + my problem...
    Code:
    Public Class clsMetaData
        Private m_metaData As String
        Public Property metaData() As String
            Get
                Return m_metaData
            End Get
            Set(ByVal value As String)
                m_metaData = value
            End Set
        End Property
    End Class
    
    Public Class myTester
        Private m_myProperty As New clsMetaData
        Public Property myProperty() As clsMetaData
            Get
                Return m_myProperty
            End Get
            Set(ByVal value As clsMetaData)
                m_myProperty = value
            End Set
        End Property
    End Class
    
    Public Class settings1
        Dim myClass1 As myTester
        Public Sub DoThis()
            myClass1.myProperty.metaData = "mySurname"
            myClass1.myProperty = "myName"
        End Sub
    End Class
    When I use [myClass1.myProperty.metaData = "mySurname"] - Hurray, it seems I have my meta data, until I try [myClass1.myProperty = "myName"] it wont allow me to do this because I am not initializing the property with a string variable.

    I cannot seem to overload this property right either because if I add in the next code section, only the return types differ, thus errors.
    Code:
        Private m_myPropertyValue As String
        Public Property myProperty() As String
            Get
                Return m_myPropertyValue
            End Get
            Set(ByVal value As String)
                m_myPropertyValue = value
            End Set
        End Property
    Thanks in advance for any advice and ideas,

    Smithy.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to create a Property.subProperty?

    I would say that a better solution would be to make two members in the metadata class, one with the name, the other with the metadata. After all, for this to work in any way, it would need to have both of those members, so why not put them into the same class?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: How to create a Property.subProperty?

    The trouble I have then is the property grid control wont be able to automate the way it accesses my list of properties.

    I need to be able to do:
    Code:
    myClass.myProperty1
    myClass.myProperty2
    it is the myClass.myProperty.metaData I am flexible with to change.
    Last edited by Smithy963; Feb 4th, 2009 at 03:21 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to create a Property.subProperty?

    First up, there's really no such thing as a subproperty. The fact that you're thinking as though there is probably making it harder to achieve your aim. If I do this:
    vb.net Code:
    1. var1 = myObject.MyProperty.MySubproperty
    then MySubproperty has no direct relationship to myObject or its type at all. myObject has a property named MyProperty and that property returns an object. That object has a property of its own named MySubproperty. MySubproperty is NOT a member of myObject. That code above is equivalent to this:
    vb.net Code:
    1. var2 = myObject.MyProperty
    2. var1 = var2.MySubproperty
    Also, you cannot overload a property that doesn't have any parameters. What defines the difference between overloads is the number, type and order of its parameters. If a property has no parameters then it can't be overloaded. The only properties that generally get overloaded are those that index a collection, e.g. the DataColumnCollection.Item property is overloaded because you can specify a numeric index or a name string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: How to create a Property.subProperty?

    I already understand why its not working,

    Thank you for your help, but is there no work around to achieve this some other way.

    currently I have created a metaData class, and the meta data is accessible like so...

    Code:
    myClass.myProperty = "string"
    myClass.MetaData.myproperty = "metaString"
    This isnt really ideal as I'm now using 2 separate objects instead of an object in an object...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to create a Property.subProperty?

    I don't believe there is any way to achieve what you want, as what I understand your needs to be is that you want myClass.myProperty to get/set a string, while myClass.myProperty.Metadata would require myProperty to get/set a metadata object. Since that would mean that myProperty would return different things in different cases, you have some pretty bad name collision.

    Aside from the option you just showed, the closest I can come to would be to move the string down into the metadata class such that myClass.MyProperty would always return a metadata object and the two versions would be:

    myClass.myProperty.myString
    -and-
    myClass.myProperty.MetaData
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: How to create a Property.subProperty?

    As I said before, I need to be able to access string properties directly so they are automated into my PropertyGrid control.

    Perhaps I will understand what I really want and how to do it someday...

    but for today I will have to deal with:
    myClass.myProperty
    -and-
    myClass.metaData.myProperty (to retrieve my properties attached metadata)

    Thanks a lot for your advice,

    Muchos gracias,

    Smithy.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to create a Property.subProperty?

    You would usually do:
    MyClass.MySubClass.MySubClassProperty

    So instead of a property what you need is a sub-class and instead of sub-property you need a property in the sub-class.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to create a Property.subProperty?

    Do something like this for what you are trying to achieve:
    vb.net Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim MyClassX As New Class2
    4.         MyClassX.MyProperty.MySubProperty = "hello"
    5.         MsgBox(MyClassX.MyProperty.MySubProperty)
    6.  
    7.     End Sub
    8. End Class
    9.  
    10. Public Class Class2
    11.     Private _MyProperty As New SubClass2
    12.     Public Property MyProperty() As SubClass2
    13.         Get
    14.             Return _MyProperty
    15.         End Get
    16.         Set(ByVal value As SubClass2)
    17.             _MyProperty = value
    18.         End Set
    19.     End Property
    20.  
    21.     Public Class SubClass2
    22.         Private _MySubProperty As String
    23.         Public Property MySubProperty() As String
    24.             Get
    25.                 Return _MySubProperty
    26.             End Get
    27.             Set(ByVal value As String)
    28.                 _MySubProperty = value
    29.             End Set
    30.         End Property
    31.     End Class
    32. End Class

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: How to create a Property.subProperty?

    Theres always somebody to jump in and raise spirits when that last shred of hope dwindles....

    I take my hat off to you, It's a little more work than I envisioned since I have around 100-200 properties, but this is so stupidly obvious I cannot believe I missed it.

    /bow

    Thank you.

    Smithy

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to create a Property.subProperty?

    I don't think what you have showed us is posible to do. Even the above example can not give you this (what you wanted in the first place).
    vb Code:
    1. myClass1.myProperty.metaData = "mySurname"
    2. myClass1.myProperty = "myName"
    In the Pradeep's example myProperty is a type of subClass and it is not a string type. Is this what you wanted? but your example above is not showing that.
    If we go with the logic of your example, it tells us that the string data type has a property called metadata which is not true. Isn’t myProperty a string type that takes “myName”?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to create a Property.subProperty?

    Quote Originally Posted by VBDT
    I don't think what you have showed us is posible to do. Even the above example can not give you this (what you wanted in the first place).
    vb Code:
    1. myClass1.myProperty.metaData = "mySurname"
    2. myClass1.myProperty = "myName"
    In the Pradeep's example myProperty is a type of subClass and it is not a string type. Is this what you wanted? but your example above is not showing that.
    If we go with the logic of your example, it tells us that the string data type has a property called metadata which is not true. Isn’t myProperty a string type that takes “myName”?
    Code:
    Public Property myProperty() As clsMetaData
    Isn't myProperty declared as a class?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to create a Property.subProperty?

    Quote Originally Posted by Pradeep1210
    Code:
    Public Property myProperty() As clsMetaData
    Isn't myProperty declared as a class?
    Pradeep, yes it is but from his example the “MyProperty” takes a string “myName” which means it spouse to be a string type. here there is a logic error. So if we let myProperty be of a type clsMetaData than how it is possible to assign to it “myName”?
    Initially as I understand he wanted to overload the property and that is not possible with properties as Jmc told it already.

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: How to create a Property.subProperty?

    I hereby decree VBDT the bringer of bad news...

    aw, VBDT your point inspired me to try out a few variations of Pradeep's solution hence my opening line

    Do you think I could just overload the property and give it parameter:

    Code:
        Public Property ServerName() As String
            Get
                Return m_HostName
            End Get
            Set(ByVal value As String)
                m_HostName = value
    
            End Set
        End Property
    
        Public Property ServerName(ByVal metaData As Boolean) As String
            Get
                Return m_HostName_metaData
            End Get
            Set(ByVal value As String)
                m_HostName_metaData = value
            End Set
        End Property
    I could then give a special attribute to the Property to hide it from the PropertyGrid control...

    Thanks for bouncing some great ideas off me guys.

    Smithy

  15. #15
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to create a Property.subProperty?

    yes, if your properties differe by the parameter they take. the first property would not conflict with the second one because the second one has a parameter and the first one doesn't. If this is good for you than it can work. Just make a use of the parmeter, let say metaData has a property and you get or set the value of that property through the second property.

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Arrow Re: How to create a Property.subProperty?

    Quote Originally Posted by VBDT
    Pradeep, yes it is but from his example the “MyProperty” takes a string “myName” which means it spouse to be a string type. here there is a logic error. So if we let myProperty be of a type clsMetaData than how it is possible to assign to it “myName”?
    Initially as I understand he wanted to overload the property and that is not possible with properties as Jmc told it already.
    Ohhh.. I see now.

    vb Code:
    1. myClass1.myProperty.metaData = "mySurname"
    2. myClass1.myProperty = "myName"
    This was something possible in VB6 using parameterless default properties. But in .NET, default properties can't be parameterless. You must provide atleast one parameter.

    So the nearest you would come to is:
    vb.net Code:
    1. 'Add this property to clsMetaData
    2. Default Public Property Prop1(ByVal n As Integer) As String
    3.     Get
    4.         'get value
    5.     End Get
    6.     Set(ByVal value As String)
    7.         'set value
    8.     End Set
    9. End Property
    10.  
    11. 'So then you can access it like this:
    12. myClass1.myProperty.metaData = "mySurname"
    13. myClass1.myProperty(0) = "myName"


    The other and better way is to add a function which accepts your string and returns a clsMetaData object:
    vb.net Code:
    1. Public Class clsMetaData
    2.     Private m_metaData As String
    3.     Public Property metaData() As String
    4.         Get
    5.             Return m_metaData
    6.         End Get
    7.         Set(ByVal value As String)
    8.             m_metaData = value
    9.         End Set
    10.     End Property
    11.  
    12.     Public Shared Function Parse(ByVal s As String) As clsMetaData
    13.  
    14.         'resolve your string to object of type clsMetaData class here
    15.  
    16.         Dim obj As New clsMetaData
    17.         Return obj
    18.     End Function
    19.  
    20. End Class
    21.  
    22. Public Class myTester
    23.     Private m_myProperty As New clsMetaData
    24.     Public Property myProperty() As clsMetaData
    25.         Get
    26.             Return m_myProperty
    27.         End Get
    28.         Set(ByVal value As clsMetaData)
    29.             m_myProperty = value
    30.         End Set
    31.     End Property
    32. End Class
    33.  
    34. Public Class settings1
    35.     Dim myClass1 As myTester
    36.     Public Sub DoThis()
    37.         myClass1.myProperty.metaData = "mySurname"
    38.         myClass1.myProperty = clsMetaData.Parse("myName")
    39.     End Sub
    40. End Class


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to create a Property.subProperty?

    That's true, as long as you are willing to have one version take a parameter and the other take no parameter, then you can overload as much as you like. It makes the code a bit obscure, though, because the parameter is meaningless.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width