Question

I am converting a large VB.Net project to C#. I used an free automated tool to convert them. VB is happy with empty parameters passed in and uses default values for them supplied in the method definition. After the conversion, there are many calls in the C# code looking like this:

GetElement(ndFirst, WAIT_AFTER_BATCH,false, , , "WAIT");

And compiler is not happy...

Can someone point me in the right direction if I can edit the C# files automatically(since intellisense knows how to) to fill these default values when I am making the call.

Was it helpful?

Solution

I am assuming that the C# version of your methods has the default parameters specified correctly, ex.

public void SomeMethod(int paramA = 0, int paramB = 1, int paramC = 2){
   ...
}

If you want to call the method and accept the default for paramB (as in your example), then you need to name the parameters

SomeMethod(paramA: 12, paramC: 20);

This would collect values for paramA and paramC whilst using the default for paramB.

Furthermore, since paramA is in the correct position, this will also be correct

SomeMethod(12, paramC: 20);

Alternatively, if the parameter you want to leave out is the last one, you can simply leave it out and call your method like so

SomeMethod(12,20);

Where paramA = 12, paramB = 20 and paramC will use the default of 2.

Unfortunately I am unaware of an automatic way to fix these. Likewise, to speed you up, you can use ReSharper and create a formatter to enforce using named arguments. Then run a simple regex over your project and clean out any instances of ',,'

Note I am in no way affiliated with JetBrains or ReSharper. I am a mere client of this company.

OTHER TIPS

I have also experience that when i shifted from VB.Net to C#.Net

Here's what you will do:

  1. First of all import InteropServices NameSpace

    using System.Runtime.InteropServices;
    
  2. Then create a function or procedure like this one below: (in my case it's a function)

    static string ResizeImage(string imgTemp,
                              [Optional, DefaultParameterValue(200)] int xLength)
    {
       // Do something HERE
    }
    
  1. I fixed the compiler errors in generated cs files for the common libraries
  2. Wrote a utility that goes over the method definitions in the file and prepared a catalog of the methods and ParamterInfos in a dictionary.

     var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
            //this is not exact for methods with different signatures.
            foreach (var methodInfo in methodInfos)
            {
                if(!methodCatalog.ContainsKey(methodInfo.Name)) 
                    methodCatalog.Add(methodInfo.Name, methodInfo.GetParameters());
                else
                {
    
                    methodCatalog.Add(methodInfo.Name + "__" + Guid.NewGuid() , methodInfo.GetParameters());
                }
            }
    
  3. Wrote another method where I tokenize a line for the method call get the method parameters (they are all "," seperated)

  4. For the empty parameter values I set the values from my method dictionary

        for(int i = 0; i < paramValues.Length; i++)
        {
            if(  string.IsNullOrEmpty( paramValues[i].Trim()) )
            {
                paramValues[i] = currentParameterInfos[i].DefaultValue.ToString();
            }
        } 
    
  5. This is error prone but it is better than goin in the files and editing them by hand.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top