Nested Property

Dark Kai

Well-known member
Joined
Sep 3, 2003
Messages
48
Hi every1, need some help here with properties.
Im trying to group my properties like the property "Size" in a form which consist of width and height.
Thanks in advance
 
You have to make your own struct or class and expose your property as one of that type.

Say your "property" is "Vector" with 3 subproperties "X", "Y" and "Z":
C#:
public struct Vector
{
    public double X;
    public double Y;
    public double Z;
}

public class MyClass
{
    public Vector Vector;
}

// Farther down in code:
MyClass c = new MyClass();
c.Vector.X = 1.0;
c.Vector.Y = 2.0;
c.Vector.Z = 3.0;

NOTE: In C# you can name a field the same as a type, as shown with "public Vector Vector" above. This is semi-common practice when you dont want to extend the Vector class but really just use it as a grouping mechanism for other properties. You cant do this in VB.NET as far as I know as it wont let you declare a variable like that (same name as the type). You might be able to name the struct "_Vector" or something similar, then name your property Vector.

-Nerseus
 
Ive tried that already, but its just a blank property at the "property manager". Sort of readonly property and there is no + next to it for me to expend the property.
 
Are you looking for something like this? Im not entirely sure what you need.
Code:
define some class
Public Class MyOwnProperty
     declare some vars
     private _width as Integer
     private _height as integer
     now declare properties
     Public Property Width As Integer
     Get
          return _width
     End Get
     Set(ByVal Value As Integer)
          _width = Value
     End Set
     End Property
     Public Property Height As Integer
     Get
          return _height
     End Get
     Set(ByVal Value As Integer)
          _height = Value
     End Set
     End Property
End Class

and now use this class as a readonly property
Public Class ClassToUseProperty
     declare the previous class that was made
     private _myproperty As New MyOwnProperty
     And use the object as a read-only property
     Public ReadOnly Property TheProperty As MyOwnProperty
     Get
           return _myproperty
     End Get
     End Property
End Class
 
Last edited by a moderator:
I tested the codes and its not working. well the results are the same as a readonly property and i cant access the Width and Height in the MyOwnProperty class.
 
Ah, you need this as a property on a Control, so that it shows up in the Properties window. Thats a bit different :)

Unfortunately, I dont know the right decoration to add to get that to work for a custom class. You might try divil as hes the resident Custom Control guru. Or try looking in the MSDN help custom attributes/decorations (things like [WebMethod] that you put before a function/property to tell the compiler to add extra info).

-Nerseus
 
Public properties, readonly or not, will show up in the properties window of a custom control without any attributes.

Some of the attributes I use in this case are DescriptionAttribute (sets the description thats shown property window), CategoryAttribute (which category it comes under) and defaultvalueAttribute (when the property = DefaultValueAttribute, its non-bold in the property window; <> default value and its shown as bold).
 
I think he wants a custom object to show up in the properties window, complete with a little "+" to allow expanding and showing the variables of the subtype. Will that happen automatically?

-Nerseus
 
Sorry, no, I havent done this before, and I think I remember having trouble with it in the past.

If I get some time Ill take a look in to it and get a solution.
 
Oh ok....Ill try looking up for more info. If any1 out there have an example on this pls let me know. Thanks to all that had contributed in this thread. :p
 
Back
Top