2

I'm converting an old application over from VB.Net to C# and got most of the converting done, but ran into an error message that should not be there (can't use "using static ..." in a couple of files, but it works fine everywhere else).

Sometimes it builds the entire solution without an error (until I run it), sometimes not. I presume this is a bug carried over since Visual Studio 2015.

The error is:

CS8026 Feature 'using static' is not available in C# 5. Please use language version 6 or greater.

I know it's a bug as DotNetFW 4.72 uses C#7.3. But I need a workaround, patch, hack or something.

I've tried changing the C# version in the Advanced settings for the application Build, but it's greyed out with the text "Automatically selected based on framework version".

I followed some steps to edit the proj file, with no luck (it DID work for every project in the solution where this same error popped up, but not the webforms project).

All projects in the solution use the same version of DNFW (4.72).

I've cleaned and rebuilt the solution, closed VS and reopened it, rebooted the computer (sometimes that helps) and am out of ideas. There's got to be a trick to force C# version 7.3 to work, somewhere, but I can't find it. What can I try next?

using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using static AppCore.Core.Xutilities;  <-- this is the problem, but only occurs on the web application

The same using statements work on the other 3 console apps and 1 web api app.

Update

Okay- the closest I am to an answer is here. A user suggested a fix, but the suggestion is too vague - so I'm experimenting. But at least I know it's truly a VS bug! :)

halfer
  • 19,824
  • 17
  • 99
  • 186
MC9000
  • 2,076
  • 7
  • 45
  • 80
  • Sounds like you need to pick a different framework version. Have you tried that? – John Wu Jun 08 '22 at 00:50
  • I will be helpful to see the code which has error. Can you share the relevant code? – Chetan Jun 08 '22 at 02:03
  • frameworks for all projects are identical (as I mentioned). The error is in a couple of files at the top with the "using static ...." statement. It's not the code, it's a configuration. – MC9000 Jun 08 '22 at 02:28
  • 1
    static is a key word to the language. If you have a namespace called static, I would not be surprised on the failure. Is it a sub-namespace where you can be more explicit, maybe even give it an alias reference? such as using MyStatic = SomeNamespace.static – DRapp Jun 08 '22 at 03:02
  • I don't have a namespace called "static". Basically, anywhere in the webforms application, VS compiler insists I'm using C#5. The other project(s) are a class library, 3 console apps, and a web API. The only CSharp files affected reside in web forms app (can't use the using static ... in the imports section on any file). I did try to set 7.3 in the csproj file, but that didn't work. It still compiles, but shows the errors (go figure), but won't run in a browser (same error(s)). – MC9000 Jun 08 '22 at 03:18
  • I made a change up top to show the using statements (and an explanation that they ONLY fail in the web application, all the other apps that share a common library are fine: in other words, the static class is working perfectly - VS has something corrupted somewhere) – MC9000 Jun 08 '22 at 03:23
  • 2
    related? : https://stackoverflow.com/questions/27968963/c-sharp-6-0-features-not-working-with-visual-studio-2015 – Mitch Wheat Jun 08 '22 at 04:09

1 Answers1

1

Okay, the solution is to add the Nuget package Microsoft.CodeDom.Providers.DotNetCompilerPlatform and make sure it's version 3.6

  • this wasn't a bug, per se, just an artifact from changing versions over time (the initial solution was made with VS2012 and has been upgraded every version since - but it was previously a VB.Net app which is why it worked before translating to C#) Works like a charm!
MC9000
  • 2,076
  • 7
  • 45
  • 80