0

In a VB.Net Structure, how do I add the XML description tag for the New() constructor with no parameters? This is so the description is displayed by Intellisense.

I created the structure shown below. It's a stripped-down version of System.Drawing.Size(). Then based on this post, I used the ''' trick (three single quotes) to automatically insert the <summary></summary> tags.

This worked great for everything but the empty New() constructor. Where do I put the summary tags for this? Perhaps I'm not doing the empty New() correctly? I did have to experiment to make it work, and ended up with Shared but no access modifier.

I've pasted together the large screenshot below to show the issue visually. Intellisense shows the descriptions for sections 1, 2, and 4. But in the 3rd section, when the empty New() signature is displayed, Intellisense does not show the description.

What have I missed?

Perhaps there is no solution in this situation?

The screenshot...

enter image description here

And the code...

'A simple structure to store a pair of integers which specify the width and height of an arbitrary
'area. This could be used for various types of tables, grids, control layouts, etc. This structure
'is based on System.Drawing.Size(), but stripped down & slightly modified.
''' <summary>
''' A simple structure to define the width and height of an arbitrary area.
''' </summary>
Public Structure Area

    'Auto-implemented properties of the Area structure.
    Public Property Width As Integer
    Public Property Height As Integer

    ''' <summary>
    ''' A simple structure to define the width and height of an arbitrary area.
    ''' </summary>
    Shared Sub New()
    End Sub

    'Initialize a new instance of the Area structure with the specified dimensions.
    ''' <summary>
    ''' A simple structure to define the width and height of an arbitrary area.
    ''' </summary>
    ''' <param name="Width">The width of the area.</param>
    ''' <param name="Height">The height of the area.</param>
    Public Sub New(Width As Integer, Height As Integer)
        Me.Width = Width
        Me.Height = Height
    End Sub

    'Return a structure with .Width=0 and .Height=0.
    ''' <summary>
    ''' A simple structure to define the width and height of an arbitrary area.
    ''' </summary>
    Public Shared ReadOnly Empty As Area

    'Tests whether this Area structure has .Width=0 and .Height=0.
    Public ReadOnly Property IsEmpty As Boolean
        Get
            Return (Me.Width = 0 AndAlso Me.Height = 0)
        End Get
    End Property

    'Tests to see whether the specified object is an Area structure with the same dimensions as this instance.
    Public Overrides Function Equals(obj As Object) As Boolean
        Dim fTest As Boolean = False
        If Me.GetType = obj.GetType Then
            Dim objArea As Area = DirectCast(obj, Area)
            If Me = objArea Then
                fTest = True
            End If
        End If
        Return fTest
    End Function

    'Returns a hash code for this Area structure.
    Public Overrides Function GetHashCode() As Integer
        Return Me.ToString().GetHashCode()
        'https://stackoverflow.com/a/11142338/2705042
    End Function

    'Return a human-readable string that represents this Area structure in the format "Width, Height"
    '(width-comma-space-height). 
    Public Overrides Function ToString() As String
        Return Width.ToString() & ", " & Height.ToString
    End Function

    'Return a human-readable string that represents this Area structure in the specified format.
    '(not yet implemented)
    'TODO: Add code here to process replaceable parameters to define the format, similar to "m/d/yyyy"
    'in Strings.Format(). 
    'Public Overloads Function ToString(ByVal format As String) As String
    '    Return Width.ToString() & ", " & Height.ToString
    'End Function

    'Tests whether two Area structures have equal dimensions.
    Public Shared Operator =(Area1 As Area, Area2 As Area) As Boolean
        If Area1.Width = Area2.Width AndAlso Area1.Height = Area2.Height Then
            Return True
        Else
            Return False
        End If
    End Operator

    'Tests whether two Area structures have different dimensions.
    Public Shared Operator <>(Area1 As Area, Area2 As Area) As Boolean
        If Area1.Width <> Area2.Width OrElse Area1.Height <> Area2.Height Then
            Return True
        Else
            Return False
        End If
    End Operator

End Structure
spinjector
  • 3,121
  • 3
  • 26
  • 56
  • 1
    You don't. Adding a `Shared` constructor is not a solution to anything relating to an instance constructor. You can't add a parameterless constructor to a structure - it's implicit - so you can't add documentation to a parameterless constructor. Notice how the parameterless constructor for the `Size` structure, which you're trying to emulate, has no documentation? – jmcilhinney Oct 25 '22 at 23:38
  • @jmcilhinney ahh yes, I see. I just used the same simple test with Drawing.Size as I did with my Area, and Size.New() has no description. Also, if I understand you correctly, adding New() to a structure is useless? Or at least useless in the Area structure which I've created? – spinjector Oct 26 '22 at 02:29
  • 1
    Not useless, impossible to do. There is a defined (implicit) parameterless ctor for `Structure`s which initializes all of the value elements to their default value. If you try to replace it with your own, you get error BC30629, "Structures cannot declare a non-shared 'Sub New' with no parameters." – Craig Oct 26 '22 at 13:32
  • @jmcilhinney ahhh yes, I did get that error, which led me to remove the access modifier, so it was just `Shared Sub New()`. But I haven't got to where I can test this class yet, so I don't know if that would've given me problems or generated errors. Thanks for your help & explanations. – spinjector Oct 26 '22 at 14:29

0 Answers0