Results 1 to 4 of 4

Thread: [RESOLVED] Setting the Attributes for Native Structures

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [RESOLVED] Setting the Attributes for Native Structures

    In a previous post (http://www.vbforums.com/showthread.php?t=626818) I needed to change how the values of an object's properties were displayed. In my case, I wanted to change how the native System.Drawing.PointF structure was displayed. If you check the post, it was resolved by using a TypeConverter.

    Now, my next question is how do I give existing properties attributes? For example, the System.Drawing.PointF structure has three properties: X, Y, and IsEmpty. I would like to give the X and Y properties descriptions. Also, I would like to hide the IsEmpty property, i.e., Browsable(False).

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Setting the Attributes for Native Structures

    You can't do that with structures I'm afraid. With classes, you can inherit them and then either override or shadow the base properties with your own and then apply whatever attributes you like. You can't inherit structures so you can't override or shadow their properties. What you may be able to do is define your own type that wraps the existing type. You then get to define all the functionality, e.g.
    vb.net Code:
    1. Public Structure MyPointF
    2.  
    3.     Private p As PointF
    4.  
    5.     Public Property X() As Single
    6.         Get
    7.             Return p.X
    8.         End Get
    9.         Set(ByVal value As Single)
    10.             p.X = value
    11.         End Set
    12.     End Property
    13.  
    14.     '...
    15.  
    16. End Structure

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Setting the Attributes for Native Structures

    What you may be able to do is define your own type that wraps the existing type
    If I create my own structure, how will methods that require the original structure be effected?

    For example, the System.Drawing.Graphics class has a DrawPolygon method that is overloaded with either Point() or PointF() as parameters. If I call DrawPolygon with MyPointF() as the parameter, it will result in an error.

    Is this another TypeConverter issue?

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

    Re: Setting the Attributes for Native Structures

    Obviously you can't use your custom type where some other type is expected. Your custom type is wrapping the other type though, so all you do is pass the wrapped value instead of the wrapper, e.g.
    vb Code:
    1. Dim p As PointF = myCustomStructure.PointF
    where that PointF property exposes the 'p' field from my previous code sample.

    The wrapper is for the benefit of the designer, so that you can expose the desired attributes to it. For anything else you use the original type.

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