locked
inline out variable initialization error! RRS feed

  • Question

  • whats wrong with this code? (visual studio 2017)

    [Route("{id:int}")]
    public IHttpActionResult Get(int id)
    {
        var opResult = this._ServiceProvider.GetById(null, id, out ServiceProvider result); // line 24
        return Ok(result);
    }

    output window:

    1>C:\Users\...Documents\Visual Studio 2015\Projects\..\..\Controllers\ProviderController.cs(24,95,24,101): error CS1003: Syntax error, ',' expected
    ========== Build: 0 succeeded, 1 failed, 4 up-to-date, 0 skipped ==========

    when I change the code to below the problem solved:

    [Route("{id:int}")]
    public IHttpActionResult Get(int id)
    {
        var ServiceProvider result;
        var opResult = this._ServiceProvider.GetById(null, id, out result);
        return Ok(result);
    }

    Wednesday, April 12, 2017 1:32 PM

Answers

  • The second block of code cannot be valid. You are specifying the type of result as both var and ServiceProvider. It would have to be ServiceProvider in this case.

    The issue is that you're using the wrong compiler. When you created your web app it would have automatically included the NuGet package for the compilers (Roslyn). When you do this it completely changes the build behavior. If you look at your project file it is now being built against the compiler that is contained in the NuGet package and not the compiler that is shipped with VS. This is what will allow you to take advantage of the newer Roslyn features in, say, your MVC views (which normally compile against the version that is part of ASP.NET in IIS).

    The problem is that the templates that ship with VS 2017 use v1.3.2 of the compiler. The features you are looking for are in the v2.x version. You need to go to Manage NuGet Packages for Solution, then go to the Updates tab and you should see an update for the compilers. Update the package and then recompile.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Rainmater Saturday, April 15, 2017 4:50 AM
    Wednesday, April 12, 2017 1:52 PM
  • i update the Microsoft.Net.Compilers NuGet package then the problem solved.
    • Marked as answer by Rainmater Saturday, April 15, 2017 4:51 AM
    Saturday, April 15, 2017 4:51 AM

All replies

  • First glance says you didn't have 

    var ServiceProvider result;

    which means result wasn't defined. 

    For using out you must look here


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 12, 2017 1:46 PM
  • Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 12, 2017 1:47 PM
  • I haven't played much with C#7, but the syntax in the first example does seem to be valid to my understanding of the language, although the first example is clearly not valid C#6.

    You appear to still be using C#6 as your compiler and not C#7.  I am guessing, based on your file path, that this was originally written in VS2015 (and hence using C#6 despite upgrading to VS2017).  Go to Project Properties->Build->Advanced and make sure you have C#7 selected as your Language Version then rebuild.




    • Edited by SimonRev Wednesday, April 12, 2017 1:50 PM
    Wednesday, April 12, 2017 1:48 PM
  • The second block of code cannot be valid. You are specifying the type of result as both var and ServiceProvider. It would have to be ServiceProvider in this case.

    The issue is that you're using the wrong compiler. When you created your web app it would have automatically included the NuGet package for the compilers (Roslyn). When you do this it completely changes the build behavior. If you look at your project file it is now being built against the compiler that is contained in the NuGet package and not the compiler that is shipped with VS. This is what will allow you to take advantage of the newer Roslyn features in, say, your MVC views (which normally compile against the version that is part of ASP.NET in IIS).

    The problem is that the templates that ship with VS 2017 use v1.3.2 of the compiler. The features you are looking for are in the v2.x version. You need to go to Manage NuGet Packages for Solution, then go to the Updates tab and you should see an update for the compilers. Update the package and then recompile.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Rainmater Saturday, April 15, 2017 4:50 AM
    Wednesday, April 12, 2017 1:52 PM
  • First glance says you didn't have 

    var ServiceProvider result;

    which means result wasn't defined. 

    For using out you must look here


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    The OP specifically says he is using VS 2017, which should be using C# 7, and this does support inline out variables (https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/)

    Unfortunately I'm not in a position to try this out myself at the moment, but wonder if the version of .Net you are targetting makes any difference. You could try creating a brand new project as a test and trying the same syntax.

    Edit: Previous posters have just pointed out you need to update the compiler being used!
    • Edited by RJP1973 Wednesday, April 12, 2017 1:54 PM
    Wednesday, April 12, 2017 1:53 PM
  • thanks for your reply.

    the code is written in vs 2017 by C# 7.0 new feature (inline parameter initialization),

    but when I build the project, visual studio show error dialog. (but there aren't any error or warning in error window)

    the important fact is that when I write my code in 2nd shape that I wrote in above, visual studio prefers me to change that to the 1st shape.


    • Edited by Rainmater Wednesday, April 12, 2017 5:02 PM
    Wednesday, April 12, 2017 5:00 PM
  • i update the Microsoft.Net.Compilers NuGet package then the problem solved.
    • Marked as answer by Rainmater Saturday, April 15, 2017 4:51 AM
    Saturday, April 15, 2017 4:51 AM