gmplOverGenericStatement
- Mark Juras
Owned by Mark Juras
Â
OverGeneric Statement Summary
OverGeneric is a terminal, refactoring statement that occurs within Refactor statements. It is used to specify that individual subprogram parameters be implemented as being generic via an overloaded wrapper 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 constraint type that can be applied to the declaration of the generic. The constaint types are as follows: Â
| ||||||||||||||
 |
The OverGeneric 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 Generic 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="Overload3" /> <Select DevEnv="VS2013" /> <Select Dialect="csh" /> <Select BuildFile="local" /> <Select DeployLocation="C:\Temp" /> <Select System="..\..\FromIdl" /> <Compile Project="..\vb6\Project1.vbp" > <Refactor> <OverGeneric identifier="Project1.Form1.MySub.o_Var" Types="Integer,String" /> </Refactor> </Compile> <Analyse /> <Output Status="New" Filename="..\csh\Overload3.bnd" /> <Author /> <Storage Action="Close" /> </gmBasic>
Â
The resultant translation is as follows.Â
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(bool i_Param1,ref object o_Var) { if (i_Param1) { o_Var = 5; } else { o_Var = "five"; } } public void MySub<T>(bool i_Param1,ref T o_Var) { object TemporaryArg1 = o_Var; MySub(i_Param1,ref TemporaryArg1); o_Var = (T)(TemporaryArg1); }
Â
Table of Contents