question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: wrong conversion of optional struct or decimal ref parameter

See original GitHub issue

VB.Net input code

Private Shared Sub OptionalParams()
    FunctionWithOptionalParams()
End Sub

 Private Shared Sub FunctionWithOptionalParams(Optional ByRef structParam As TestStruct = Nothing, Optional ByRef decimalParam As Decimal = 0)
    structParam = New TestStruct
    decimalParam = 0
 End Sub

Friend Structure TestStruct
    Friend A As Boolean
End Structure

Erroneous output

 private static void OptionalParams()
 {
      TestStruct argstructParam = null;
      decimal argdecimalParam = 0m;
      FunctionWithOptionalParams(structParam: ref argstructParam, decimalParam: ref argdecimalParam);
 }

private static void FunctionWithOptionalParams([Optional, DefaultParameterValue(default(TestStruct))] ref TestStruct structParam, [Optional, DefaultParameterValue(0m)] ref decimal decimalParam)

Expected output

private static void OptionalParams()
{
    TestStruct argstructParam = default;
}

private static void FunctionWithOptionalParams([Optional] ref TestStruct structParam, [Optional, DefaultParameterValue(0)] ref decimal decimalParam)

Details

  • Product in use: VS extension
  • Version in use: 9.0.0.0
  • Did you see it working in a previous version, which? no
  • may be, there is a better way to express DefaultParameterValue for a structure

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
KunzeAndreascommented, May 5, 2022

private static void FunctionWithOptionalParams(ref TestStruct structParam, ref decimal decimalParam, ref string strParam, ref DateTime dateParam)

would break VB code calling converted C# code (in VB you can omit optional ref params)

1reaction
KunzeAndreascommented, May 7, 2022

Optional ByRef xxx As Short = 0S converts to [Optional, DefaultParameterValue(0)] out short xxx

should be [Optional, DefaultParameterValue((short)0)] out short xxx

Read more comments on GitHub >

github_iconTop Results From Across the Web

VB -> C#: Convert optional ref parameters · Issue #91
In C# A ref or out parameter cannot have a default value Input code Public Class OptionalRefIssue91 Public Shared Function TestSub(Optional ...
Read more >
A property or indexer may not be passed as an out or ref ...
Property or indexer may not be passed as an out or ref parameter at compile time. I even tried this: double.TryParse(objReader[i].ToString().
Read more >
C#/VB struct – how to avoid case with zero default values ...
A constructor would perform the conversion from an input maxLength , or you could provide a private setter to keep the 10 localized...
Read more >
Overload resolution
This chapter describes the rules that govern overload resolution when multiple members have the same name.
Read more >
Conversions - C# language specification
This chapter covers the possible conversions from one type to another in C#. Builtin conversions, user defined conversions, implicit and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found