gmplGenericStatement
- Mark Juras
Owned by Mark Juras
Generic Statement Summary
Generic is a terminal, refactoring statement that occurs within Refactor statements. It is used to specify that individual subprogram parameters be implemented as being generic to accept a list of different types. The attributes of the Generic statement are as follows:Attribute | Description | ||||||||||||||
Identifier | This required attribute specifies the symbol, the actual parameter to be made generic. If the containing Refactor statement had a FileFilter specified then this identifier should be specified relative to it; else, it should be specified relative to the root of the symbol table | ||||||||||||||
Types | This optional attribute contains a comma-delimited list of the types to be allowed for the generic parameter. | ||||||||||||||
Constraint | This optional parameter specifies a constring type that can be applied to
the declaration of the generic. The constaint types are as follows:
| ||||||||||||||
The Generic statement is especially important for dealing with ByRef parameters that have Variant or Object types. Note that this statement is closely related to the Overload and OverGeneric refactoring statements which also deal with these types of parameters. Consider the following simple VB6 code
Private Sub Form_Load() Dim fiveString As String Dim fiveNumber As Integer MySub False, fiveString MySub True, fiveNumber Dim sixNumber As Integer sixNumber = 1 + fiveNumber MsgBox "Five plus one is " & sixNumber Dim sixString As String sixString = "one plus " & fiveString MsgBox "In other words six is equal to " & sixString End Sub Private Sub MySub(ByVal i_Param1 As Boolean, ByRef o_Var As Variant) If i_Param1 Then o_Var = 5 Else o_Var = "five" End If End Sub
<gmBasic> <Storage Action="Create" Identifier="Overload" /> <Select DevEnv="VS2013" /> <Select Dialect="csh" /> <Select BuildFile="local" /> <Select DeployLocation="C:\Temp" /> <Select System="..\..\FromIdl" /> <Compile Project="..\vb6\Project1.vbp" > </Compile> <Analyse /> <Output Status="New" Filename="..\csh\Overload.bnd" /> <Author /> <Storage Action="Close" /> </gmBasic>
private void Form_Load(object sender, EventArgs e) { string fiveString = ""; int fiveNumber = 0; int refTemp1 = Convert.ToInt32(fiveString); <======= MySub(false,ref refTemp1); MySub(true,ref fiveNumber); int sixNumber = 0; sixNumber = 1 + fiveNumber; System.Windows.Forms.MessageBox.Show("Five plus one is " + sixNumber, "", MessageBoxButtons.OK); string sixString = ""; sixString = "one plus " + fiveString; System.Windows.Forms.MessageBox.Show("In other words six is equal to " + sixString, "", MessageBoxButtons.OK); } private void MySub(bool i_Param1,ref int o_Var) <====== { if (i_Param1) { o_Var = 5; <====== } else { o_Var = Convert.ToInt32("five"); } }
<FixType identifier="Project1.Form1.MySub.o_Var" Type="Variant" status="ObjectOnly" />
private void Form_Load(object sender, EventArgs e) { string fiveString = ""; int fiveNumber = 0; object argTemp1 = fiveString; MySub(false,ref argTemp1); object argTemp2 = fiveNumber; MySub(true,ref argTemp2); int sixNumber = 0; sixNumber = 1 + fiveNumber; System.Windows.Forms.MessageBox.Show("Five plus one is " + sixNumber, "", MessageBoxButtons.OK); string sixString = ""; sixString = "one plus " + fiveString; System.Windows.Forms.MessageBox.Show("In other words six is equal to " + sixString, "", MessageBoxButtons.OK); } private void MySub(bool i_Param1,ref object o_Var) { if (i_Param1) { o_Var = 5; } else { o_Var = "five"; } }
<gmBasic> <Storage Action="Create" Identifier="Overload2" /> <Select DevEnv="VS2013" /> <Select Dialect="csh" /> <Select BuildFile="local" /> <Select DeployLocation="C:\Temp" /> <Select System="..\..\FromIdl" /> <Compile Project="..\vb6\Project1.vbp" > <Refactor> <Generic identifier="Project1.Form1.MySub.o_Var" Types="Integer,String" /> </Refactor> </Compile> <Analyse /> <Output Status="New" Filename="..\csh\Overload2.bnd" /> <Author /> <Storage Action="Close" /> </gmBasic>
private void Form_Load(object sender, EventArgs e) { string fiveString = ""; int fiveNumber = 0; MySub(false,ref fiveString); MySub(true,ref fiveNumber); int sixNumber = 0; sixNumber = 1 + fiveNumber; System.Windows.Forms.MessageBox.Show("Five plus one is " + sixNumber, "", MessageBoxButtons.OK); string sixString = ""; sixString = "one plus " + fiveString; System.Windows.Forms.MessageBox.Show("In other words six is equal to " + sixString, "", MessageBoxButtons.OK); } private void MySub<T>(bool i_Param1,ref T o_Var) { if (i_Param1) { o_Var = (T)(object)5; } else { o_Var = (T)(object)"five"; } }
Table of Contents