-1

Looking for some vb to c# translation help.

The immediate line below in is causing an error in C# ("argument 1 must be passed with the 'ref' keyword") - adding a 'ref" before nextbutton.visible causes yet another error ("A property or indexer may not be passed as an out or ref parameter"). Any suggestions is appreciated.

nextButton.Enabled = InlineAssignHelper(nextButton.Visible, false);

private static T InlineAssignHelper<T>(ref T target, T value)
{
     target = value;
     return value;
}

Here is the VB.net code we are trying to convert used with no errors:

nextButton.Enabled = InlineAssignHelper(nextButton.Visible, False)

Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
midnite11
  • 81
  • 1
  • 10
  • 1
    Just remove the _ref_ an it will work as VB – Steve Nov 10 '20 at 18:51
  • In c# add Ref : InlineAssignHelper(ref nextButton.Visible, false); – jdweng Nov 10 '20 at 18:53
  • 2
    @jdweng no it will not work. Still a property and cannot be passed by ref. https://stackoverflow.com/questions/1402803/passing-properties-by-reference-in-c-sharp – Steve Nov 10 '20 at 18:58
  • 1
    The VB compiler emits code to allow properties to be passed as `ByRef` method arguments. Just create a temp variable before the call and pass that variable; then after the call assign the variable back to the property. – TnTinMn Nov 10 '20 at 19:01
  • @Steve - removing the "ref" in the InlineAssignHelper() removed the error. We have work to do still to test, but thus far looks good. Thank you. – midnite11 Nov 10 '20 at 19:11
  • 1
    However in C# that code doesn't change the Visible property to false. – Steve Nov 10 '20 at 19:34

1 Answers1

0

Looking at the logic of InlineAssignHelper, I am wondering which issues it might actually solve for you. I personally would not use it.

Perhaps this C# expression works just as fine:

nextButton.Enabled = nextButton.Visible = false;

Or you could just use two separate statements (which has my personal preference and would also just work fine in VB.NET):

nextButton.Enabled = false;
nextButton.Visible = false;

But these are just my two cents to your issues at hand.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22