The following code for a co-worker throws the following error when he tries to compile it using VS 2008:
Error:
A new expression requires () or [] after type
Code:
MyClass Structure:
public class MyClass
{
public MyClass() {}
public string Property1 { get; set; }
public string Property2 { get; set; }
}
Sample Source Code:
List<MyClass> x = new List<MyClass>();
x.Add(new MyClass
{
Property1 = "MyValue",
Property2 = "Another Value"
});
It "works on my machine", but not his. Any idea why?
UPDATE
He is targeting the 3.5 .NET framework
He is using the System.Collections.Generics namespace
The MyClass object does have a constructor
UPDATE 1:
@Funky81 - Your example and my example were able to compile on my PC.
Update 2:
Included schema of MyClass in sample
UPDATE 3:
@DK - I had my co-worker add the following configuration section to his application:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
And he received the following compilation error: Unrecognized element 'providerOption'.
Here's what seems to be the only similar, but not exactly the same, error available in VS.2008:
Compiler Error CS1526 : A new expression requires (), [], or {} after type
Note those {}
in error message, which are part of c# 3.0 syntax. This is not related to framework version, but to the version of the language.
My bet is that somehow a different version of compiler was used.
Added: this looks like a likely issue with ASP.Net. Place to check is in .config file(s), element
configuration\system.codedom\compilers\compiler @language="c#..."
there should be
<providerOption name="CompilerVersion" value="v3.5"/>
It gives me bad compile message in my PC
Try this
x.Add(new MyClass()
{
Property1 = "MyValue",
Property2 = "Another Value"
});
Notice, there is another bracket after MyClass class creation.
Is his project targetting .NET 3.5? If not, that error would be thrown on the x.Add(new MyClass line because the new class doesn't have a constructor or indexer specified.
Are you sure you're using the exact same code?
The code you supplied doesn't compile, because it lacks a declaration of MyClass
. Please supply us with a full compiland, not just a code snippet. Then send that file to your friend to make sure you still see the different behavior on your machine.
The error message points to a line & column, right? Tell us where.
Are you both at the same .NET service pack level? I got bitten today because one machine was .NET 3.5 RTM, the other was .NET 3.5 SP1. In .NET 3.5 SP1 (which also installs .NET 2.0 SP2), they introduced a new overload to the System.Web.Caching.Cache.Insert method which I used on my dev box and thenfailed on an intermediate pre-build environment:
public void Insert(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemUpdateCallback onUpdateCallback
)
Took me a few mins to workout why this was broken on one machine but not the other...
Update: the error message 'A new expression requires () or [] after type' often means you've missed a constructor parens. Are you sure you haven't missed a () off of the line:
List<MyClass> x = new List<MyClass>();
Or somewhere near by?
Update Again: I have built the following:
using System;
using System.Collections.Generic;
namespace Test
{
public class MyClass
{
public MyClass() { }
public string Property1 { get; set; }
public string Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<MyClass> x = new List<MyClass>();
x.Add(new MyClass
{
Property1 = "Kev",
Property2 = "Kev 2"
});
}
}
}
VS2008 SP1 Targetting 3.5 - Compiles ok
VS2008 SP1 Targetting 3.0 - Compiles ok
VS2008 SP1 Targetting 2.0 - Compiles ok
VS2008 RTM - Targetting 3.5 - Compiles ok
VS2008 RTM - Targetting 3.0 - Compiles ok
VS2008 RTM - Targetting 2.0 - Compiles ok
VS2005 - 8.0.50727.867 (on machine with VS2008/3.5 SP1) - Fails with:
A new expression requires () or [] after type
'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern
VS2005 - 8.0.50727.762 (on machine with VS2008/3.5 RTM) - Fails with:
A new expression requires () or [] after type
'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern
I am inclined to think the problem is not with the code you are presenting unless a screen shot is supplied with an example of the compile error using the above code or similar. Any chance of boiling things down to something simple to eliminate red herrings?
Cheers
Kev
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With