C# out parameter does not convert right into VB.NET

3 posts, 0 answers
  1. Matti
    Matti avatar
    2 posts
    Member since:
    Aug 2010

    Posted 06 Aug 2010 Link to this post


    I just encountered a problem with your Batch Converter concerning how the out-parameter of C# is converted into VB.NET.

    Namely, I noticed that when in the C# method-calling there is out parameter included (for example):

         PrintControl(c, ParentControlPrinting.BeforeChilds, mp, x, y, ref extendedHeight, out scanForChildControls);

    ... then the corresponding conversion in VB.NET functions not right - because the Sub method that is being called should
    have ByRef keyword for the corresponding  parameter (here scanForChildControls) to function properly - at least in
    many cases. But your converter sets the keyword ByVal and ignores the out parameter.

    Could this problem be solved?


    Matti Ylén
  2. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 11 Aug 2010 Link to this post

    As far as I'm aware, the SharpDevelop NRefactory engine (which we use for conversions) does not have support for converting the C# "out" keyword. It will, however, try to convert "ByRef" VB keywords to "ref" C# keywords in method declarations. For a quick test with the converter offered by SharpDevelop, you can follow this link:


    We'll do some additional research, and if a solution can be found, we'll definitely update the converter. Thanks for highlighting the issue!

  3. Matti
    Matti avatar
    2 posts
    Member since:
    Aug 2010

    Posted 14 Aug 2010 Link to this post

    ...Thanks Todd for your time with the issue.

    I tested the SharpDevelop NRefactory engine via the link you provided and indeed encountered the same problem: out -parameter of C# is not converted into ByRef-keyword for the VB.Net's parameter definition in the sub procedure or function being called. But, as you said, the ref -paramteter of C#  is indeed converted correctly into ByRef. Below I give a piece of C# code to demostrate this for yourself:

            private void pd_PrintPage(object sender, PrintPageEventArgs ev)
                Single extendedHeight;
                Single y;

                y = 0;
                extendedHeight = 0;
                bool scanForChildControls;
                if (DelegatePrintingReportTitle == null)
                    PrintReportTitle(_f, ParentControlPrinting.BeforeChilds , _MultiPage, _xform, y, ref extendedHeight, out scanForChildControls);
                    DelegatePrintingReportTitle(_f, ParentControlPrinting.BeforeChilds , _MultiPage, _xform, y, ref extendedHeight, out scanForChildControls);
                y += extendedHeight;

                // Print each control on the form
                Single globalExtendedHeight;
                PrintControls(_f, _MultiPage, _xform, y, out globalExtendedHeight);

                if (_MultiPage.LastPage())
                    ev.HasMorePages = false;
                    ev.HasMorePages = true;

    public void PrintReportTitle(System.Windows.Forms.Control c,
                ParentControlPrinting typePrint,
                MultiPageManagement mp,
                Single x, Single y,
                ref Single extendedHeight, out bool ScanForChildControls)
                ScanForChildControls = false;
                Font printFont = new Font(c.Font.Name, (Single) (c.Font.Size * 1.3), FontStyle.Bold);
                float fontHeight =  mp.FontHeight(printFont);
                Pen pen = new Pen(Color.Black, 2);
                extendedHeight = fontHeight + 3 + pen.Width + 1;

                mp.BeginPrintUnit(y, extendedHeight);
                mp.DrawString(c.Text, printFont, Brushes.Black, x, y, c.Width, fontHeight);
                y += fontHeight + 3;
                mp.DrawLines(pen, x, y, x + c.Size.Width, y);


    Matti Ylén
Back to Top