[Mercury Feature request] Typed Structures

I want to see typed structures of which the elements can be constants, variables (including read-only and array), read-only properties, other structures, and custom data types.

Also… VB currently allows you to declare a variable/constant of a structure type only at the module and local levels. I want to be able to declare global variables of a structure (and enumeration) type.

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/structures

welcome @GypsyPrince2k

So you mean - everything that you can do in a class?

Can you give a code example of what you have in mind?

Here is an example of a custom type called ‘CharTypes’ using constants…

Friend Structure CharTypes 'Character types...
        ''' <summary>Character type; control.</summary>
        Friend Const CHR_CONTROL As System.Int64 = 3
        ''' <summary>Character type; drawing.</summary>
        Friend Const CHR_DRAWING As System.Int64 = 5
        ''' <summary>Character type; textual.</summary>
        Friend Const CHR_TEXTUAL As System.Int64 = 1
        ''' <summary>Character type; unknown character or error.</summary>
        Friend Const CHR_UNKNOWN As System.Int64 = 0
        ''' <summary>Character type; whitespace.</summary>
        Friend Const CHR_WSPACE As System.Int64 = 2
        ''' <summary>Character type; whitespace and control.</summary>
        Friend Const CHR_WSPCCTRL As System.Int64 = 4
    End Structure

Here is an example of a custom type called ‘CtrlChars’ using read-only variables…

Friend Structure CtrlChars 'Control characters...
        ''' <summary>Control character; acknowledgment.</summary>
        Friend Shared ReadOnly CTL_AKNWLDGMNT As System.Char = Convert.ToChar(6)
        ''' <summary>Control character; backspace.</summary>
        Friend Shared ReadOnly CTL_BACKSPACE As System.Char = Convert.ToChar(8)
        ''' <summary>Control character; bell.</summary>
        Friend Shared ReadOnly CTL_BELL As System.Char = Convert.ToChar(7)
        ''' <summary>Control character; cancel.</summary>
        Friend Shared ReadOnly CTL_CANCEL As System.Char = Convert.ToChar(24)
        ''' <summary>Control character; carriage return.</summary>
        Friend Shared ReadOnly CTL_CRGRTRN As System.Char = Convert.ToChar(13)
        ''' <summary>Control string; carriage return + line feed - MS Windows dependent.</summary>
        Friend Shared ReadOnly CTL_CRLF As System.String = Convert.ToChar(13) + Convert.ToChar(10)
        ''' <summary>Control character; data link.</summary>
        Friend Shared ReadOnly CTL_DATALINK As System.Char = Convert.ToChar(16)
End Structure

What is the reason behind this ? Because you can do this with a Shared Class.
The only real difference is that the class is heap allocated and the structure is stack allocated.
(Only trying to understand the reasoning)

About the global variables - Can you do that? How?

Here is an example of one using read-only properties…

      ''' <summary>Returns information regarding the most previous exception or runtime error.</summary>
            Public Structure PreviousError
                ''' <summary>Returns an indication of whether the previous adverse event is expected (an exception) or is unexpected (a runtime error).</summary>
                ''' <value>Returns a boolean value of True if the event is expected (an exception). Returns a value of False if the event is unexpected (a runtime error). Returns a value of False in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property Expected() As System.Boolean = m_blnExpP
                ''' <summary>Returns the name of the data field (variable, constant, type structure, type enumeration, etc.) involved with the previous exception or error.</summary>
                ''' <summary>Returns the path to the previous exception or error's corresponding help file.</summary>
                ''' <value>Returns a string value containing the path to the help file. Returns an empty value if no path is specified. Returns an empty value in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property HelpFile() As System.String = m_strHlpP
                ''' <summary>Returns a topic keyword within an associated help file, the topic of which corresponds to the previous exception or error.</summary>
                ''' <value>Returns a string value containing the help topic keyword for the exception or error. Returns an empty value if no keyword is specified. Returns an empty value in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property HelpKeyword() As System.String = m_strKeyP
                ''' <summary>Returns the name of the object or code library within which the previous exception or error is raised.</summary>
                ''' <value>Returns a string value containing the name of the object or code library in which the exception or error is raised. Returns an empty value if the source cannot be determined. Returns an empty value in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property Library() As System.String = m_strLibP
                ''' <summary>Returns a unique numeric code used to identify the previous exception or error.</summary>
                ''' <value>Returns an integer value containing a numeric code which corresponds to the exception or error. Returns a value of zero if no error is raised. Returns a value of zero in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property Number() As System.Int64 = m_lngNumP
                ''' <summary>Returns the name of the routine, property, or event which raised the previous exception or error.</summary>
                ''' <value>Returns a string value containing the name of the routine that raised the exception or error. Returns an empty value if the source cannot be determined. Returns an empty value in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property Routine() As System.String = m_strRtnP
                ''' <summary>Returns text which briefly summarizes the previous exception or error.</summary>
                ''' <value>Returns a string value containing the text summary of the exception or error. Returns an empty value if no description is determined. Returns an empty value in the event of an error.</value>
                ''' <remarks></remarks>
                Public Shared ReadOnly Property Summary() As System.String = m_strSumP
End Structure

You can’t currently do global variables that are declared as a structure type in VB. I would not have the first idea of how to implement such a feature.

No, I mean a normal Global variable - not a structure.
Because I do not know how to do that.

Oh. In VB you can declare a global variable using the “Public” keyword as long as it is one of the intrinsic (built-in) data types or managed object. i.e., String, Integer, Long, etc…

Just declare it in any standard module. You can’t declare a global variable in a form or class module.

Public m_frmMain As System.Windows.Forms.Form 'A proxy instance of the main form object.

Hmm,

Just tried - and it all works in VB 2019.
So, I think I do not understand correctly.

Can you send me a simple project with the not working code?
You can just past the zip in this forum.

Oh? It may work now, I haven’t tried. I know that at one time you could not create a global variable using a custom type.

In fact, the Microsoft documentation still says module and local scope variables.

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/structure-variables

image

The purpose of creating structures is to minimally encapsulate related constants, variables, etc. and create namespaces for them without the burden of creating a class.

Friend Structure Names
    ''' <summary>Names; first name.</summary>
    Friend Const FirstName As System.String = "Gypsy"
    ''' <summary>Names; last name.</summary>
    Friend Const LastName As System.String = "Prince"
End Structure

Dim namFirst As Names
Dim namLast As Names

namFirst = Names.FirstName
namLast = Names.LastName

MsgBox("My name is " & namFirst & namLast & ".") 'Displays "My name is GypsyPrince."

Allows for very efficient programming

Is your example in Mercury or VB?

My example was VB, not Mercury.
To see if something should work, I use VB, because VB is leading unless it is new functionality.