1

By automatic conversion of code written in VB.NET to C# I have such situation in function declaration of VB.NET:

Private Function dataArchiver(ByVal toPlace As String, 
                               Optional ByVal aExtension As String = ".7z", 
                               Optional ByRef createdName As String = "") As Integer

Tool for automatic conversion does this in C#:

private int dataArchiver(string toPlace, 
                         string aExtension = ".7z", 
                         ref string createdName = "")

And this of course don't work. Keyword "ref" before last argument is underlined with red. Why is that so? Because string createdName may be (and don't have to be) generated in function and in that case have to be passed out from function. It is important that this code can work with NET framework 3.5.

Any idea to get this working in C# without much reconcepting of VB.NET program?

Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • 2
    I do not think C# supports default values for `ref` params. They are different languages after all. The solution? Assume a `null` value or create an overload of `dataArchiver` that passes an empty string. – User 12345678 Aug 18 '14 at 18:05
  • Yes thanks, not so elegant but it seem's with overloading that can be solved. – Wine Too Aug 18 '14 at 19:31
  • Use a different tool in the future - converting to [ref string createdName = ""] is a lousy conversion (since it cannot compile). – Dave Doknjas Aug 18 '14 at 19:38
  • Are you sure the original VB.NET code actually _needed_ the string to be `ByRef`? Does the method or something it calls actually change the reference to the string? So that if the caller passed `myCreatedName` as that parameter, could `myCreatedName` be different after the call to `dataArchiver`? – John Saunders Aug 18 '14 at 19:40
  • Yes, inside dataArchiver createdName can be changed and after leaving this function program should know about that if it is changed or more precise created (not empty). – Wine Too Aug 18 '14 at 19:46

2 Answers2

1

You can use the out keyword instead in this case:

string createdName;
int retVal = dataArchiver("to place", out createdName, extension);

And your new method signature.

private int dataArchiver(string toPlace, 
                      out string createdName,
                      string aExtension = ".7z")
{
    createdName = "some value";

    // rest of your code
}

The only change is that out can't have a method signature default value, so you will need to set it inside the method.

siva.k
  • 1,344
  • 14
  • 24
  • I'm not sure if that will suit all needs and I can't test that since here is still much of issues generated with automatic converting. Do you maybe know which equivalent for keyword "out" is in Visual Basic NET? – Wine Too Aug 18 '14 at 19:30
  • There isn't anything in VB that's comparable, but because of how `ByRef` works in VB it seems pretty interchangeable. http://stackoverflow.com/a/4358774/1561465 – siva.k Aug 18 '14 at 19:41
1

You have to create overloaded methods for this (as you would have had to do before C# acquired the optional parameter feature):

private int dataArchiver(string toPlace, string aExtension)
{
    string tempVar = "";
    return dataArchiver(toPlace, aExtension, ref tempVar);
}

private int dataArchiver(string toPlace)
{
    string tempVar = "";
    return dataArchiver(toPlace, ".7z", ref tempVar);
}

private int dataArchiver(string toPlace, string aExtension, ref string createdName)
{
    return 0;
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28