[mono-vb] VB Class status

Jambunathan Jambunathan kjambunathan@novell.com
Thu, 24 Jun 2004 02:14:25 -0600


Rafael

> What makes that .param [n] appear there?=20
> [opt]  comes from the Optional attribute? it seems so, but ...

( All these observations based on Windows XP box)
[opt] does get generated by [Optional]
.param does get generated by System.ComponentModel.DefaultValue.
Interestingly however, for the HasDefault flag to be turned on
the compiler apparently looks for ".param[] =3D  " construct.

Not sure what would segment of C# code would force
emitting of ".param[] =3D " construct. I believe that there is no such
construct in C# though.

Anyways the functionality is not broken ( if you use vbc as opposed to =
mbas )=20
only that the MSVB class status page simply refuse to recognize all the=20
good effort that we have put in ...

Should corcompare be changed to accomodate this ? Shouldn't be a big deal
I think.

Regards,
Jambunathan K.


ps:

Just out of curisoity I did this:
Compiled the following SampleClass.cs

using System;
using System.Runtime.InteropServices;
using  System.ComponentModel;
using System.Reflection;

class SampleClass
{
	public static void Main()
	{
		Assembly thisAss =3D Assembly.GetExecutingAssembly();
		Type type =3D thisAss.GetType("SampleClass");
		MethodInfo method =3D type.GetMethod("SampleMethod");

		ParameterInfo [] parameters =3D method.GetParameters();
		foreach(ParameterInfo param in parameters)
		{
			Console.WriteLine("param name =3D {0}, param =
attribute =3D {1}",
                                                                           =
     param.Name, param.Attributes);
		}
	}
=09
	public static void SampleMethod(int a, [Optional][DefaultValue(10)]=
 int b)
	{
		Console.WriteLine(b);
	}
}

I see that parameter b has Optional bit turned on but not the HasDefault =
bit.
The IL code for the above exe looks like this.

  .method public hidebysig static void  SampleMethod(int32 a,  [opt] int32 =
b) cil managed  =20
{     .param [2]  // notice that there is no "=3Dsomething" =20
      .custom instance void [System]System.ComponentModel.DefaultValueAttri=
bute::.ctor(int32) =3D ( 01 00 0A 00 00 00 00 00 )     =20
      // Code size       7 (0x7)     .maxstack  1    =20
     IL_0000:  ldarg.1    =20
     IL_0001:  call      =20
     void [mscorlib]System.Console::WriteLine(int32)    =20
      IL_0006:  ret  =20
} // end of method SampleClass::SampleMethod

If in the above IL segment, I replace .param [2] with .param[2]=3D0x0000000=
A
and comment out .custom instance blah blah and compile the modified IL
back to an exe and execute the same I see that the parameter b has now
both Optional and HasDefault flags turned on.

>> "A Rafael D Teixeira" <rafaelteixeirabr@hotmail.com> 6/24/2004 1:13:54 =
AM >>>
Inline

>From: "Jambunathan Jambunathan" <kjambunathan@novell.com>
>
>The errors in Microsoft.VisualBasic may seem intimidating at first.
>Most of the errors are due to a mismatch in the Parameter Attributes.
>
>Many VB functions/methods support optional parameters with a default
>value. MS's dll marks the Parameter with Optional, HasDefault
>attributes.
>
>In our C# implementation of the method, we mark such parameters using
>the
>following two attributes: System.Runtime.InteropServices.Optional
>and System.ComponentModel.DefaultValue(). As a result Mono's dll has
>Parameter Attributes just set to Optional ( but has no HasDeafult ).
>
>I am not sure whether this is bug or a baggage not carried from the
>past.
>( Quick googling suggested that just using C# it's not possible to
>create a parameter with Parameter Attributes set to Optional |
>HasDeafult.
>
>Just posting to the mailing list with the hope that I will get a quick
>response.

It's indead trickier:

code like

   Private Sub x(ByVal w As String, Optional ByVal y As Integer =3D 132)
        TextBox1.Text =3D w & y
    End Sub

    Private Sub z()
        x("what", 1)
        x("whatnot")
    End Sub

generates things like

.method private instance void  x(string w,
                                 [opt] int32 y) cil managed
{
  .param [2] =3D int32(0x00000084)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
...
  IL_001a:  ret
} // end of method Form1::x

<comments>
we need to generate it properly for MS.VB.Dll but we still need more=20
information to know how. What makes that .param [n] appear there? [opt]=20
comes from the Optional attribute? it seems so, but ...

That is why I guessed DefaultValue, but it seems it is not the proper =
thing=20
to use...

Maybe our truly Jackson of mono=B4s ilasm fame can bear some light for =
us...
</comments>

and

.method private instance void  z() cil managed
{
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldstr      "what"
  IL_0007:  ldc.i4.1
  IL_0008:  callvirt   instance void TesteXmlNText.Form1::x(string,
                                                            int32)
  IL_000d:  nop
  IL_000e:  ldarg.0
  IL_000f:  ldstr      "whatnot"
  IL_0014:  ldc.i4     0x84
  IL_0019:  callvirt   instance void TesteXmlNText.Form1::x(string,
                                                            int32)
  IL_001e:  nop
  IL_001f:  nop
  IL_0020:  ret
} // end of method Form1::z

<comments>
that mbas must take care to generate, see that the compiler discover =
the=20
default value and pass it
to the method, but it complicates even more when you add overload-resolutio=
n=20
to the figure, and also late-binding (we will have to implement all =
that=20
policies inside the helping classes in MS.VB.dll also).
</comments>

So indeed a long way to go,

Have fun boys,

Rafael "Monoman" Teixeira
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/=20
MonoBrasil Founding Member - Membro Fundador do MonoBrasil=20
http://monobrasil.softwarelivre.org=20
English Blog: http://monoblog.blogspot.com/=20
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/=20

_________________________________________________________________
MSN Messenger: instale gr=E1tis e converse com seus amigos.=20
http://messenger.msn.com.br=20